1 引言:
在数据解析过程中,json格式的数据多用来做前后端数据交互、接口数据交互等,弄清楚Java语言中解析json嵌套数组对于数据交互有着重要意义,本文基于自己的代码编写和逻辑梳理,整理出本篇干货,亲测有效。
2 理论:
2-1 json字符串和json对象的区别:
//格式区别:
//JSON字符串:
var str='{"name":"xiaoming","age":24}';
//JSON对象:
var obj={"name":"xiaoming","age":24};
2-2 简单的解析json字符串:
//首先将json字符串转换为json对象:
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
//然后再解析json对象,根据json中的键得到它的值:
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
int sex = jsonObject.getInt("sex");
int age = jsonObject.getInt("age");
2-3 将json字符串转换为java对象:
//将json字符串转换为json对象:
JSONObject obj = JSONObject.fromObject(jsonStr);
//将建json对象转换为Person对象:
Person jb = (Person)JSONObject.toBean(obj,Person.class);
2-4 将java对象转换为json字符串:
//将java对象转换为json对象:
JSONObject json = JSONObject.fromObject(obj);
//将json对象转换为字符串:
String str = json.toString();
2-5 Java解析json嵌套数组:
//先转换外层数组:
JSONArray jsonarray = JSONArray.fromObject(jsonStr);
for(int i=0;i<jsonarray.size();i++){
//遍历外层数组,拿到当前的数组对象
JSONObject jsonObject=jsonarray.getJSONObject(i);
...
}
2-6 java中JSON将数组对象转换成JSON字符串输出实例:
ArrayList <Student> students=new ArrayList<Student>();
students.add(new Student("xiaoming",24));
students.add(new Student("xiaohong",26));
students.add(new Student("xiaocao",22));
JSONArray result = JSONArray.fromObject(students);
System.out.println(result);
3 实践:
举例:
3.1 解析复杂的json数据:
数据格式如下:(特点:json中有json数组,数组中又嵌套json数组)
【注意:json数组和json字符串的区别,这个在测试的时候要注意,否则会报格式错误。】
{
"team": "AB-CD-12-34",
"Student_num": "3",
"Student_list": [{"Study_id":"1","Student_name":"小明","Subject_num":"2","Subject_list":[{"name":"语文","grade":"98"},{"name":"数学","grade":"92"}]},{"Study_id":"2","Student_name":"小红","Subject_num":"2","Subject_list":[{"name":"语文","grade":"93"},{"name":"数学","grade":"99"}]},{"Study_id":"3","Student_name":"小曹","Subject_num":"2","Subject_list":[{"name":"语文","grade":"90"},{"name":"数学","grade":"97"}]}]
}
代码参考:
String jsonRequest = getJsonRequest(request);
//从输入参数的String字符串中解析出json对象的每个参数:
//将字符串--->json对象:
JSONObject object = JSONObject.fromObject(jsonRequest);
//获取team:
String team = object.get("String").toString();
//获取Student_num:
String Student_num = object.get("Student_num").toString();
//获取Student_list:
String Student_list = object.get("Student_list").toString();
//解析外层Student_list,实际是一个json数组:
JSONArray jsonArray = JSONArray.fromObject(Student_list);
if(jsonArray.size()>0){
for(int i=0;i<jsonArray.size();i++){
//把每一个取出的json数组内容转成json对象:
JSONObject job = jsonArray.getJSONObject(i);
//获取Study_id:
String Study_id = job.get("Study_id").toString();
//获取Student_name:
String Student_name = job.get("Student_name").toString();
//获取Subject_num:
String Subject_num = job.get("Subject_num").toString();
//获取Subject_list:
String Subject_list = job.get("Subject_list").toString();
//解析内层Subject_list,实际是一个json数组:
JSONArray jsonArraySub = JSONArray.fromObject(Subject_list);
if(jsonArraySub.size()>0){
for(int j=0;j<jsonArraySub.size();j++){
//把每一个取出的json数组内容转成json对象:
JSONObject jobSub = jsonArrayCell.getJSONObject(j);
//之后的思路同外层json的解析:
......
}
}
}
}
3.2 存储复杂的json数据:
数据格式同上:
【这一步牵扯到怎样将得到的结果按照小类和大类分别写到不同的json和json数组中,有可能json数组中还有json数组。】
代码参考:(仅思路)
Map<String,Object> map = new HashMap<String, Object>();
map.put("team", (V) team);
map.put("Student_num", (V) Student_num);
//这里是处理Student_list的代码:(具体获得的数据需要根据实际业务改动代码)
List <Map<String,Object>> Student_list_temp=new ArrayList<>();
for(int i=0;i<List.size();i++){
Map<String,Object> mapTemp = new HashMap<String, Object>();
mapTemp.put("Study_id", (V) Study_id);
mapTemp.put("Student_name", Student_name);
mapTemp.put("Subject_num", Subject_num);
//这里是处理Subject_list的代码:(具体获得的数据需要根据实际业务改动代码)
List <Map<String,Object>> Subject_list_temp=new ArrayList<>();
for(int j=0;j<SubList.size();j++){
Map<String,Object> mapSubTemp = new HashMap<String, Object>();
mapSubTemp.put("name", name);
mapSubTemp.put("grade", grade);
Subject_list_temp.add(mapSubTemp);
}
JSONArray Subject_list = JSONArray.fromObject(Subject_list_temp);
mapTemp.put("Subject_list", (V) Subject_list);
Student_list_temp.add(mapTemp);
}
JSONArray Student_list = JSONArray.fromObject(Student_list_temp);
map.put("Student_list", (V) Student_list);
JSONObject jsonObject = JSONObject.fromObject(map);
result = jsonObject.toString();
out.print(result);
参考资料:
https://blog.csdn.net/qq_36411874/article/details/87002233 java对象与json对象间的相互转换