赞
踩
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);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。