当前位置:   article > 正文

String转换成json_如何将多个string转化为json

如何将多个string转化为json

String转换成json格式(一):net.sf.json.JSONObject

原创 2016年05月04日 15:17:04

·         29304

 

 

实例:

 

[html] view plain copy

1.   //将str转换成JSONObjct格式  

2.   public static void main(String [] args){  

3.           String str = "{\"result\":\"success\",\"message\":\"成功!\"}";  

4.           JSONObject json = JSONObject.fromObject(str);  

5.           System.out.println(json.toString());  

6.       }  

 

结果:

 

 

 

实例:

[html] view plain copy

1.   //将str转换成JSONArray  

2.   public static void main(String [] args){  

3.           String str = "{\"result\":\"success\",\"message\":\"成功!\",\"data\":[{\"name\":\"Tom\",\"age\":\"20\"}]}";  

4.           JSONObject json = JSONObject.fromObject(str);  

5.           System.out.println(json);  

6.           JSONArray jsonArray = JSONArray.fromObject(json.getString("data"));  

7.           System.out.println(jsonArray.toString());  

8.       }  

 

 

结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

import org.json.JSONException;

import org.json.JSONObject;

 

  public static void main(String[] args) throws JSONException {

//   Map<String, Object> paramMap = new HashMap<String, Object>();

//   paramMap.put("userName", "sj1001");

//   paramMap.put("password", "aq0rivBIGJEWeMjoTmK4vA==");

     //String retStr = HttpClientUtils.doPost("http://192.168.1.121:8080/nwjc/user/login", "{\"userName\":\"sj1001\",\"password\":\"aq0rivBIGJEWeMjoTmK4vA==\"}", null, "utf-8");

     //System.out.println(retStr);

    

     String str = "{\"result\":\"success\",\"message\":\"成功!\",\"data\":[{\"name\":\"Tom\",\"age\":\"20\"}]}";

     JSONObject json = new JSONObject(str);

     System.out.println(json);

  }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/1014139
推荐阅读
相关标签
  

闽ICP备14008679号