1. 程式人生 > >用fastjson解析巢狀json資料

用fastjson解析巢狀json資料

json資料類似:

{
  "code": 0,
  "data": {
    "city": {
      "cityId": 284609,
      "counname": "中國",
      "name": "東城區",
      "pname": "北京市"
    },
    "liveIndex": {
      "2016-09-01": [
        {
          "day": "2016-09-01",
          "desc": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。",
          "name": "化妝指數",
          "status": "控油"
        },
        {
          "day": "2016-09-01",
          "desc": "感冒容易發生,少去人群密集的場所有利於降低感冒的機率。",
          "name": "感冒指數",
          "status": "易發"
        },
        {
          "day": "2016-09-01",
          "desc": "洗車後,可保持2天車輛清潔,比較適宜洗車。",
          "name": "洗車指數",
          "status": "較適宜"
        },
        {
          "day": "2016-09-01",
          "desc": "易感人群應適當減少室外活動。",
          "name": "空氣汙染擴散指數",
          "status": "中"
        },
        {
          "day": "2016-09-01",
          "desc": "天氣較熱,衣物精幹簡潔,室內酌情新增空調衫。",
          "name": "穿衣指數",
          "status": "熱"
        },
        {
          "day": "2016-09-01",
          "desc": "輻射較弱,塗擦SPF12-15、PA+護膚品。",
          "name": "紫外線指數",
          "status": "弱"
        },
        {
          "day": "2016-09-01",
          "desc": "天氣較好,且紫外線輻射不強,適宜戶外運動。",
          "name": "運動指數",
          "status": "適宜"
        },
        {
          "day": "2016-09-01",
          "desc": "氣壓小幅波動,可能會影響魚兒的進食。",
          "name": "釣魚指數",
          "status": "較適宜"
        }
      ]
    }
  },
  "msg": "success",
  "rc": {
    "c": 0,
    "p": "success"
  }
}
先匯入fastjson依賴:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.33</version>
    </dependency>

java拿出需要的資料:
public static City getCity(String json){
        JSONObject jsonObject = (JSONObject) JSONObject.parseObject(json)
                .getJSONObject("data")
                .getJSONObject("city");
        City city = new City();
        city.setCityId(Integer.parseInt(String.valueOf(jsonObject.get("cityId"))));
        city.setCityName((String) jsonObject.get("pname"));
        city.setCunName((String)jsonObject.get("counname"));
        city.setDistrictName((String)jsonObject.get("name"));
        return city;
    }

    public static List<Life> getLife(String lifeJson){
        List list = new ArrayList();
        JSONObject jsonObject = (JSONObject) JSONObject.parseObject(lifeJson)
                .getJSONObject("data")
                .getJSONObject("liveIndex");
        JSONArray jsonArray = jsonObject.getJSONArray(getFormatDate());
        for(int i = 0; i < 8; i++){
            Life life = new Life();
            life.setDay(new Date());
            life.setDesc(jsonArray.getJSONObject(i).getString("desc"));
            life.setName(jsonArray.getJSONObject(i).getString("name"));
            life.setStatus(jsonArray.getJSONObject(i).getString("status"));
            life.setCity(getCity(lifeJson));
            list.add(life);
        }
        return list;
    }