JSON解析详解
1.定义一串测试JSON
{ "retCode":0, "retMSg":"成功", "data":[ { "name":"李雷", "id":"001", "score":{ "Chinese":"80", "Math":"95" }, "schoolInfo": [ { "School_name": "清华" }, { "School_name": "北大" } ], }, { "name":"韩梅梅", "id":"002", "score":{ "Chinese":"90", "Math":"80" }, "schoolInfo": [ { "School_name": "清华" }, { "School_name": "北大" } } ] }
2.JSON分析:
2.1 [ ]括号代表一个数组
2.2 { }大括号代表的是一个对象,在一个对象中还可能包含对象的属性,属性也可能是另外一个类
2.3 " "双引号代表的是属性值
2.4 : 冒号代表的是前后之间的关系,冒号前面是属性名称,后面是属性的值,这个值可以是基本数据类型,也可以是引用数据类型。
以上述代码为例,第一层是一个大括号({ })的JSON数组,JSON数组中包含retCode,retMsg以及data三个属性。
第二层是data的JSONArray属性,每个大括号代表一个对象,每个对象中有name,id,score,schoolinfo四个属性。
第三层其中score属性下包含一个类,类中包含Chinese和Math两个属性。
schoolinfo是一个JSONArray数组,里面有School_name这个属性。
3.JSON各种对象之间的转换(fastjson)
3.1 String——>JSONArray
String st = "[{name:Tim,age:25,sex:male},{name:Tom,age:28,sex:male},{name:Lily,age:15,sex:female}]";
JSONArray jsonarray = JSONArray.parseArray(st);
3.2 JSONArray——>JSONObject
JSONObject jsonobject = new JSONObject();
for(int i=0;i<jsonarray.length();i++){
jsonobject = jsonarray.getJSONObject[i];
}
3.3 String——>JSONObject
String st = "{name:Tim,age:25,sex:male}";
JSONObject jsonobject = JSONObject.parseObject(st);
3.4 JSONObject——>JSONArray
JSONObject jsonobject = {info:[{name:Tim,age:25,sex:male},{name:Tom,age:28,sex:male},{name:Lily,age:15,sex:female}]};
JSONArray jsonarray = jsonobject.get("info");
4.JSON提供的方法
JSONObject系统自带的解析方式解析,我们先来JSONObject(系统自带的类)类中的方法
其中JSONObject常用的构造方法是:
JsonObject.parseObject("text");
最常用的取值方法是:
JsonObject.getString("key");
JsonObject.getJsonArray("key");
5.JSON数组解析
对result中的每一个值进行取到以及解析
先将String类型的result转为JSON数组类型
JsonObject jsonobject = JsonObject.parseObject(result);
基本数据类型:
String returncode = jsonobject.getString("result中的属性");
引用数据类型:
JsonArray parseArray = jsonobject.getJsonArray("result中的数组类型");
6.解析过程
import com.alibaba.fastjson.Json;
import com.alibaba.fastjson.JsonArray;
import com.alibaba.fastjson.JsonObject;
JsonObject jsonObject=JSONObject.parseOject(result);
String returnCode=jsonObject.getString("retCode");
String returnMsg=jsonObject.getString("retMSg");
JSONArray parseArray=jsonObject.getJsonArray("data");
for(int i=0;i<parseArray.size();i++){
JsonObject eachObject=parseArray.getJSONObject(i);
String name=eachObject.getString("name");
String id=eachObject.getString("id");
JSONObject sorceObject=eachObject.getJSONObject("sorce");
String Chinese=sorceObject.getString("Chinese");
String Math=sorceObject.getString("Math");
JSONArray schoolInfoArray = eachObject.getJSONArray("schoolInfo");
for (int j = 0; j < schoolInfoArray.length(); j++) {
JSONObject schoolInfojson = schoolInfoArray.getJSONObject(j);
String schoolName = schoolInfojson.getString("School_name");
}
}
}
7.JsonObject赋值过程
JsonObject jsonObject=new JsonObject();
JsonArray applist=new JsonArray();
JsonObject obj=JsonObject.parseObject("{
"name":"李雷",
"id":"001",
"score":{
"Chinese":"80",
"Math":"95"
},
"schoolInfo": [
{
"School_name": "清华"
},
{
"School_name": "北大"
}
],
},
{
"name":"韩梅梅",
"id":"002",
"score":{
"Chinese":"90",
"Math":"80"
},
"schoolInfo": [
{
"School_name": "清华"
},
{
"School_name": "北大"
}
}");
applist.add(obj);
jsonObject.put("retCode",0);
jsonObject.put("retMSg","成功");
jsonObject.put("data",applist);
)
8.使用HashMap来构建JsonObject
private static void createJsonByMap(){
Map<String,Object> testMap = new HashMap<String,Object>();
Object nullObj = null;
testMap.put("name","王小二");
testMap.put("age",25);
testMap.put("brithday","1900-01-01");
testMap.put("school","蓝翔");
testMap.put("major",new String[] {"理发","挖掘机"});
testMap.put("has_girlfriend",false);
testMap.put("car",nullObj);
testMap.put("house",nullObj);
testMap.put("comment","注释");
System.out.println(new JsonObject(testMap).toString());
}
9.使用JavaBean来创建对象
public class people{
private String name;
private String school;
private String has_girlfriend;
private String age;
private String car;
private String house;
private String[] major;
private String comment;
private String brithday;
}
public static void createJsonByBean(){
People r = new People();
r.setName("王小二");
r.setAge(25);
r.setBirthday("1900-01-01");
r.setSchool("蓝翔");
r.setMajor(new String[] {"理发","挖掘机"});
r.setHas_girlfriend(false);
r.setCar(null);
r.setHouse(null);
r.setComment("注释");
System.out.println(new JsonObject(r));
}
10.前后端Json交互
public JsonResult saveEmoji(@RequestParam(value = "params") String jsString){
JSONObject jsonObject= JSON.parseObject(jsString);
if(jsonObject==null){
return JsonResult.error("输入参数为空");
}
Emoji emoji=jsonObject.toJavaObject(Emoji.class);
String userid=String.valueOf(emoji.getUserid());
}
|