JSON數據解析

JSON數據的數據格式類似於map,詳細格式在這裏就詳細說了。

以下面這個JSON數據爲例說明一下解析JSON的一種方法。

{
    "head": {
        "tip": "success",
        "result": 0
    },
    "boxs": [
        {
            "picture": null,
            "boxId": 1,
            "boxUrl": "jjjjjjj",
            "boxType": "00",
            "updateTime": "2015-11-15T17:51:30",
            "status": "-1",
            "description": null,
            "boxName": "學三智能存取櫃1",
            "longitude": null,
            "boxCode": "2230",
            "latitude": null,
            "boxAddr": "北京郵電大學"
        },
        {
            "picture": null,
            "boxId": 2,
            "boxUrl": "jjjjjjj",
            "boxType": "00",
            "updateTime": "2015-11-15T15:20:38",
            "status": "-1",
            "description": null,
            "boxName": "學一智能存儲櫃",
            "longitude": null,
            "boxCode": "2231",
            "latitude": null,
            "boxAddr": "北京郵電大學"
        },
        {
            "picture": null,
            "boxId": 3,
            "boxUrl": "jjjjjjj",
            "boxType": "00",
            "updateTime": "2015-11-15T15:20:43",
            "status": "-1",
            "description": null,
            "boxName": "學二智能存取櫃",
            "longitude": null,
            "boxCode": "2232",
            "latitude": null,
            "boxAddr": null
        }
    ]
}
這個Json字符串包含了兩個對象,他們的鍵分別是“head”和一個"boxs",其中head的值是又是一個JSON對象,boxs的值是一個JSON對象的數組(JSONArray)。

package com.lql.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {



	public static List<Map<String, Object>> parseStringToObject(String jsonStr) {
		List<Map<String, Object>> datas = new ArrayList<Map<String,Object>>();
		try {
			JSONObject jsonObject = new JSONObject(jsonStr);//將JSON字符串轉換爲JSON對象
			//首先根據這個JSON對象鍵"head"所對應的子JSON對象
			JSONObject head = jsonObject.getJSONObject("head");
			Map<String, Object> map = parseHeadMap(head);
			datas.add(map);
			
			//boxs又對應了若干個JSON對象,所以通過鍵"boxs"得到一個JSON對象數組
			JSONArray boxsJson = jsonObject.getJSONArray("boxs");
			//遍歷每個JSON對象,對各個JSON對象進行解析
			for(int i = 0 ; i < boxsJson.length() ; i++){
				JSONObject jsonObj = boxsJson.getJSONObject(i);
				map = parseJsonToBox(jsonObj);
				datas.add(map);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datas;
	}
<span style="white-space:pre">	</span>//解析head所對應的JSON對象
	public static Map<String, Object> parseHeadMap(JSONObject jsonObj) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Object resValue = jsonObj.get("result");
			map.put("result", resValue);
			Object tipValue = jsonObj.get("tip");
			map.put("tip", tipValue);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	//噹噹前的JSON對象已經是沒有嵌套子JSON對象時,就直接通過各個鍵獲取每個字段的值
	public static Map<String, Object> parseJsonToBox(JSONObject jsonObj){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Object picValue = jsonObj.get("picture");
			Object boxId = jsonObj.get("boxId"); 
			Object boxUrl = jsonObj.get("boxUrl");
			Object boxType = jsonObj.get("boxType");
			Object updateTime = jsonObj.get("updateTime");
			Object status = jsonObj.get("status");
			Object description = jsonObj.get("description");
			Object boxName = jsonObj.get("boxName");
			Object longitude = jsonObj.get("longitude");
			Object boxCode = jsonObj.get("boxCode");
			Object latitude = jsonObj.get("latitude");
			Object boxAddr = jsonObj.get("boxAddr");
			map.put("picture", picValue);
			map.put("boxId", boxId);
			map.put("boxUrl", boxUrl);
			map.put("boxType", boxType);
			map.put("updateTime", updateTime);
			map.put("status", status);
			map.put("description", description);
			map.put("boxName", boxName);
			map.put("longitude", longitude);
			map.put("boxCode", boxCode);
			map.put("latitude", latitude);
			map.put("boxAddr", boxAddr);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return map;
	}

}

這個只是JSON解析的一種方法,還有其他方法比如Gson,fastjson等


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章