java解析复杂的json字符串
Java解析Json字符串--复杂对象(方法一)
{
"name": "三班",
"students": [
{
"age": 25,
"gender": "female",
"grades": "三班",
"name": "露西",
"score": {
"网络协议": 98,
"JavaEE": 92,
"计算机基础": 93
},
"weight": 51.3
},
{
"age": 26,
"gender": "male",
"grades": "三班",
"name": "杰克",
"score": {
"网络安全": 75,
"Linux操作系统": 81,
"计算机基础": 92
},
"weight": 66.5
},
{
"age": 25,
"gender": "female",
"grades": "三班",
"name": "莉莉",
"score": {
"网络安全": 95,
"Linux操作系统": 98,
"SQL数据库": 88,
"数据结构": 89
},
"weight": 55
}
]
}
我们来解析一下这个Json字符串。
首先, 最外层由一个大括弧”{}”包裹,那么我们对应的可以建一个实体类来对应,内部有两个元素,元素name比较明显是字符串类型的,元素students的值是由一个中括弧”[]”包裹,那么它对应的应该是一个数组或者列表。
接下来, 我们分析一下中括弧”[]”内部内容格式,中括弧内有三个大括弧包裹着的内容,并且内容格式基本相同,那么我们可以再定义一个实体类来对应这些大括弧。但是我们发现三个大括弧中的内容还是有些区别的,元素score对应的值虽然都是由一个大括弧来包裹,但是里边key的名称和数量都不相同。这样的情况我们不能很好的定义一个实体类来对应这个大括弧内的内容,不过我们可以定义一个Map来对应这个大括弧内的内容。
最终, 定义出来的实体类如下。
Grades.Java
package com.bean;
import java.util.List;
public class Grades {
private String name;//班级名称
private List<Student> students;//班里的所有学生
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
Student.java
package com.bean;
import java.util.Map;
/**
* 学生
*/
public class Student {
private int age;//年龄
private String gender;//性别,male/female
private String grades;//班级
private String name;//姓名
private Map<String, Integer> score;//各科分数
private float weight;//体重
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGrades() {
return grades;
}
public void setGrades(String grades) {
this.grades = grades;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Integer> getScore() {
return score;
}
public void setScore(Map<String, Integer> score) {
this.score = score;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
定义完之后,解析过程就非常简单了:
package com.test;
import net.sf.json.JSONObject;
import com.bean.Grades;
public class Domain {
public static void main(String[] args) {
String jsonStr = "{\"name\":\"三班\",\"students\":[{\"age\":25,\"gender\":\"female\",\"grades\":\"三班\",\"name\":\"露西\",\"score\":{\"网络协议\":98,\"JavaEE\":92,\"计算机基础\":93},\"weight\":51.3},{\"age\":26,\"gender\":\"male\",\"grades\":\"三班\",\"name\":\"杰克\",\"score\":{\"网络安全\":75,\"Linux操作系统\":81,\"计算机基础\":92},\"weight\":66.5},{\"age\":25,\"gender\":\"female\",\"grades\":\"三班\",\"name\":\"莉莉\",\"score\":{\"网络安全\":95,\"Linux操作系统\":98,\"SQL数据库\":88,\"数据结构\":89},\"weight\":55}]}";
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
Grades grades = (Grades) JSONObject.toBean(jsonObject, Grades.class);
}
}
Java解析Json字符串--复杂对象(方法二)
1. JSON 字符串,如下所示:
{"DevEUI_uplink": { "Time": "2018-11-03T12:46:27.665+08:00", "DevEUI": "1000FF000B000002", "FPort": "2","FCntUp": "5","ADRbit": "1", "MType": "4","FCntDn": "6", "payload_hex": "810100ffff0c4b4807000003f7f985ffffffff1401000005040101bb2d", "mic_hex": "b3aed8f1","Lrcid": "00000065","LrrRSSI": "-100.000000","LrrSNR": "1.500000", "SpFact": "11","SubBand": "G0","Channel": "LC3","DevLrrCnt": "3","Lrrid": "00010016","Late": "0","LrrLAT": "0.000000","LrrLON": "0.000000", "Lrrs": { "Lrr": [ {"Lrrid": "00010016","Chain": "0","LrrRSSI": "-100.000000","LrrSNR": "1.500000","LrrESP": "-102.324738"}, {"Lrrid": "0001001A","Chain": "0","LrrRSSI": "-107.000000","LrrSNR": "-2.000000","LrrESP": "-111.124428"}, {"Lrrid": "00010014","Chain": "0","LrrRSSI": "-109.000000","LrrSNR": "-14.500000","LrrESP": "-123.651421"} ] }, "CustomerID": "100000017", "CustomerData": {"alr":{"pro":"FOXCONN/dBoard","ver":"1"}}, "ModelCfg": "0", "DevAddr": "0326001C" } }
2. 解析成Javabean:
注意:对于复杂的json字符串解析,有时候,net.sf.json.JSONObject解析会失败,可以使用 com.google.gson.Gson解析
import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @Test public void testParseJsonStr() { String jsonStr = "{\"DevEUI_uplink\": {\"Time\": \"2018-11-03T12:46:22.382+08:00\",\"DevEUI\": \"1000FF000B000002\",\"FPort\": \"2\",\"FCntUp\": \"4\",\"ADRbit\": \"1\",\"MType\": \"4\",\"FCntDn\": \"5\",\"payload_hex\": \"810100ffff0c4b4807000003f7f985ffffffff14010000040301011890\",\"mic_hex\": \"7873fd23\"," + "\"Lrcid\": \"00000065\",\"LrrRSSI\": \"-100.000000\",\"LrrSNR\": \"2.500000\",\"SpFact\": \"11\",\"SubBand\": \"G0\",\"Channel\": \"LC4\",\"DevLrrCnt\": \"5\",\"Lrrid\": \"00010016\",\"Late\": \"0\",\"LrrLAT\": \"0.000000\",\"LrrLON\": \"0.000000\"," + "\"Lrrs\": {\"Lrr\": [{\"Lrrid\": \"00010016\",\"Chain\": \"0\",\"LrrRSSI\": \"-100.000000\",\"LrrSNR\": \"2.500000\",\"LrrESP\": \"-101.937759\"},{\"Lrrid\": \"0001001E\",\"Chain\": \"0\",\"LrrRSSI\": \"-102.000000\",\"LrrSNR\": \"-1.750000\",\"LrrESP\": \"-105.972855\"},{\"Lrrid\": \"0001000F\",\"Chain\": \"0\",\"LrrRSSI\": \"-105.000000\",\"LrrSNR\": \"-15.000000\",\"LrrESP\": \"-120.135208\"}]},\"CustomerID\": \"100000017\",\"CustomerData\": {\"alr\":{\"pro\":\"FOXCONN/dBoard\",\"ver\":\"1\"}},\"ModelCfg\": \"0\",\"DevAddr\": \"0326001C\"}}"; JSONObject jsonObj = JSONObject.fromObject(jsonStr); Object object = jsonObj.get("DevEUI_uplink"); //System.out.println("****" + object.toString()); JsonObject obj = new JsonParser().parse(object.toString()).getAsJsonObject(); ReceviceDataModel rd1 = new Gson().fromJson(obj, ReceviceDataModel.class); System.out.println("---rd1:" + rd1.toString()); LrrModel[] lrrModels = rd1.getLrrs().get("Lrr"); System.out.println("----" + lrrModels[1].toString()); }
3. JavaBean定义:
public class ReceviceDataModel extends BaseDtoAdapter { private static final long serialVersionUID = -3587750514661171087L; private Date Time; private String DevEUI; private Integer FPort; private Integer FCntUp; private Integer FCntDn; private String ADRbit; private String MType; private String payload_hex; private String mic_hex; private String Lrcid; private String LrrRSSI; private String LrrSNR; private String SpFact; private String SubBand; private String Channel; private String DevLrrCnt; private Map<String, LrrModel[]> Lrrs; private String CustomerID; private Map<String, AlrModel> CustomerData; private String ModelCfg; private String DevAddr; get.... set.... } public class LrrModel extends BaseDtoAdapter { private static final long serialVersionUID = 3105745288168791895L; private String Lrrid; private String Chain; private String LrrRSSI; private String LrrSNR; private String LrrESP; get.... set.... } public class AltModel extends BaseDtoAdapter { private static final long serialVersionUID = 7019821138967165678L; private String pro; private String ver; get... set... }