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字符串。html

首先, 最外層由一個大括弧」{}」包裹,那麼咱們對應的能夠建一個實體類來對應,內部有兩個元素,元素name比較明顯是字符串類型的,元素students的值是由一箇中括弧」[]」包裹,那麼它對應的應該是一個數組或者列表。java

接下來, 咱們分析一下中括弧」[]」內部內容格式,中括弧內有三個大括弧包裹着的內容,而且內容格式基本相同,那麼咱們能夠再定義一個實體類來對應這些大括弧。可是咱們發現三個大括弧中的內容仍是有些區別的,元素score對應的值雖然都是由一個大括弧來包裹,可是裏邊key的名稱和數量都不相同。這樣的狀況咱們不能很好的定義一個實體類來對應這個大括弧內的內容,不過咱們能夠定義一個Map來對應這個大括弧內的內容。數據庫

最終, 定義出來的實體類以下。json

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:post

注意:對於複雜的json字符串解析,有時候,net.sf.json.JSONObject解析會失敗,能夠使用 com.google.gson.Gson解析ui

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...
}
相關文章
相關標籤/搜索