区别
两个功能差不多,但是属于不同的包,parseObject是alibaba.fastjson.JSON,fromObject是net.sf.json
JsonObject.parseObject方法
第一种数据格式:
[
{
"data ":[
{
"building_id ":"*** ",
"building_num ":"** ",
"door_name ":"** ",
"electric ":"** ",
"room_name ":"** "
}
],
"success ":true
}
]
String s="[{\"success\":true,\"data\":[{\"building_id\":\"***\",\"building_num\":\"**\",\"room_name\":\"**\",\"door_name\":\"**\",\"electric\":\"**\"}]}]" ;
String b= s.substring(0,s.length()-1);
String c=b.substring(1, b.length());
System.out.println(b+"b___");
JSONObject jsonx = JSON.parseObject(c);
JSONArray ja = jsonx.getJSONArray("data");
for (int i = 0; i < ja.size(); i++) {
JSONObject jo = ja.getJSONObject(i);
String building_id = jo.getString("building_id");
System.out.println(building_id+"building_id>>>>>");
}
第二种数据格式:
[
{
"password ":"*1234567890 ",
"success ":"true "
}
]
String s="[{\"success\":\"true\",\"password\":\"*1234567890\"}]";
String b= s.substring(0,s.length()-1);
String c=b.substring(1, b.length());
System.out.println(c+"c___");
JSONObject reagobj = JSONObject.fromObject(c);
String name = reagobj.getString("password");
System.out.println(name+"name,,,,,,");
String password = jm.getString("password");
System.out.println(password);
System.out.println("看看有没有值"+password);
第三种数据格式:
{
"data ":{
"access_token ":"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206 ",
"expires_second ":36000
},
"rlt_code ":"HH0000 ",
"rlt_msg ":"成功 "
}
String res="{\"data\":{\"access_token\":\"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206\",\"expires_second\":36000},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";
JSONObject jsonObject= JSON.parseObject(res);
String data = jsonObject.getString("data");
JSONObject jsondata= JSON.parseObject(data);
String token = jsondata.getString("access_token");
第四种数据格式:
{
"data ":{
"total ":23,
"start ":0,
"total_page ":3,
"rows ":[
{
"op_way ":"3 ",
"user_mobile ":"15321918571 ",
"op_time ":1493881391976,
"pwd_no ":30
},
{
"op_way ":"1 ",
"op_time ":1493880995000,
"pwd_no ":31
}
],
"current_page ":1,
"page_size ":10
},
"rlt_code ":"HH0000 ",
"rlt_msg ":"成功 "
}
String res="{\"data\":{\"total\":23,\"start\":0,\"total_page\":3,\"rows\":[{\"op_way\":\"1\",\"op_time\":1493884964000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493884615032,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493883836552,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493883294000,\"pwd_no\":31},{\"op_way\":\"1\",\"op_time\":1493883256000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493883015371,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493882007000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881498520,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881391976,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493880995000,\"pwd_no\":31}],\"current_page\":1,\"page_size\":10},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";
JSONObject jsonObject= JSON.parseObject(res);
String data = jsonObject.getString("data");
JSONObject jsonObjects= JSON.parseObject(data);
JSONArray ja = jsonObjects.getJSONArray("rows");
for (int i = 0; i < ja.size(); i++) {
JSONObject jo = ja.getJSONObject(i);
String op_way = jo.getString("op_way");
String op_time = jo.getString("op_time");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(op_time);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
String pwd_no = jo.getString("pwd_no");
String user_mobile = jo.getString("user_mobile");
System.out.println(op_way+res+pwd_no+user_mobile+"------------");
}
1.对象与字符串之间的互转
①将对象转换成为字符串
String str = JSON.toJSONString(infoDo);
②字符串转换成为对象
InfoDo infoDo = JSON.parseObject(strInfoDo, InfoDo.class);
2.对象集合与字符串之间的互转
①将对象集合转换成为字符串
String users = JSON.toJSONString(users);
②将字符串转换成为对象集合
List<User> userList = JSON.parseArray(userStr, User.class);
3.字符串互转JSONObject
①String 转 Json对象
JSONObject jsonObject = JSONObject.parseObject(jsonString);
②json对象转string
JSONObject jsonObject = JSONObject.parseObject(str);//json对象转字符串
String jsonString = jsonObject.toJSONString();
4.map与字符串之间互转
① 字符串转map
JSONObject jsonObject = JSONObject.parseObject(str);
Map<String,Object> map = (Map<String,Object>)jsonObject;// //json对象转Map
②map转字符串
String jsonString = JSON.toJSONString(map);
5.Map 转 Json对象
①map转json对象
Map<String,Object> map = new HashMap<>();
map.put("age", 24);
map.put("name", "cool_summer_moon");
JSONObject json = new JSONObject(map);
②json对象转Map
Map<String,Object> map = (Map<String,Object>)jsonObject;
JsonObject.fromObject方法
{
"resultcode":"200",
"reason":"成功的返回",
"result":{
"company":"顺丰",
"com":"sf",
"no":"575677355677",
"list":[
{
"datetime":"2013-06-25 10:44:05",
"remark":"已收件",
"zone":"台州市"
},
{
"datetime":"2013-06-25 11:05:21",
"remark":"快件在 台州 ,准备送往下一站 台州集散中心 ",
"zone":"台州市"
}
],
"status":1
},
"error_code":0
}
8 //复杂的json数据
9 String jsonStr = "{\"resultcode\":\"200\",\"reason\":\"成功的返回\",\"result\":{\"company\":\"顺丰\",\"com\":\"sf\","
10 + "\"no\":\"575677355677\",\"list\":[{\"datetime\":\"2013-06-25 10:44:05\",\"remark\":\"已收件\",\"zone\":\"台州市\"},"
11 + "{\"datetime\":\"2013-06-25 11:05:21\",\"remark\":\"快件在 台州 ,准备送往下一站 台州集散中心 \",\"zone\":\"台州市\"}],\"status\":1},"
12 + "\"error_code\":0}";
13 JSONObject json = JSONObject.fromObject(jsonStr); //得到整个json串
14 System.out.println("resultcode:"+json.getString("resultcode")); //根据key得到value:200
15 System.out.println("reason:"+json.getString("reason")); //根据key得到value:成功的返回
16
17 //当遇到result时,也是将它当成一个整体串
18 System.out.println("company:"+JSONObject.fromObject(json.getString("result")).getString("company"));
19 System.out.println("com:"+JSONObject.fromObject(json.getString("result")).getString("com"));
20 System.out.println("no:"+JSONObject.fromObject(json.getString("result")).getString("no"));
21
22 //当遇到再次嵌套时,此时的list是一个JSONArray,所以需要将其当作数组的形式处理,其实还是一个串
23 //get(i)以下标为主,里面的东西,其实也是一个json的形式数据,你可以不看外面怎么嵌套,按道理只当成单单一个json串处理即可
24 System.out.println("list(0).datetime:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(0)).getString("datetime"));
25
26 System.out.println("list(0).remark:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(0)).getString("remark"));
27
28 System.out.println("list(0).zone:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(0)).getString("zone"));
29
30 System.out.println("list(1).datetime:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(1)).getString("datetime"));
31
32 System.out.println("list(1).remark:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(1)).getString("remark"));
33
34 System.out.println("list(1).zone:"+JSONObject.fromObject(JSONArray.fromObject(JSONObject.fromObject(json.getString("result")).getString("list")).get(1)).getString("zone"));
35
36 System.out.println("status:"+JSONObject.fromObject(json.getString("result")).getString("status"));
37
38 System.out.println("reason:"+json.getString("error_code"));
转载链接:
https://www.jb51.net/article/137445.htm
https://www.cnblogs.com/angelye/p/7997678.html