JSON使用阿里的fastJson爲依賴包html
gradle依賴管理以下:java
compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41'
一、String轉JSONObjectjson
前言:String 是JSONObject格式的字符串數組
eg:gradle
JSONObject jSONObject = JSONObject.parseObject(String);
二、String轉JSONArrayspa
前言:String 是JSONArray格式的字符串code
eg:htm
JSONArray jsonArray= JSONArray.parseArray(String);
三、JSONObject中的數組提取爲JSONArrayblog
eg:字符串
{ "AreaName": "北京", "CityId": 110100, "NoMarket": false, "OldCityId": 646, "Pinyin": "beijing", "ProvinceId": 110000, "Result": [ { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "Stelvio 鉅惠23.4萬起", "Url": "//www.autohome.com.cn/market/201904/100223763.html" }, { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "馬駒橋林肯中心年中大促", "Url": "//www.autohome.com.cn/market/201906/100230932.html" }, { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "星越平價銷售13.58萬元起", "Url": "//www.autohome.com.cn/dealer/201906/367011492.html" }, { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "哈弗F5限時優惠8000元", "Url": "//www.autohome.com.cn/dealer/201906/366897778.html" }, { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "購元新能源價格暫無優惠", "Url": "//www.autohome.com.cn/dealer/201906/366897034.html" }, { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "瑞虎3xe冰點價促銷中!", "Url": "//www.autohome.com.cn/dealer/201906/366889724.html" }, { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "購奔奔EV現鉅惠5.1萬元", "Url": "//www.autohome.com.cn/dealer/201906/366843204.html" }, { "ItemName": "優惠", "ItemUrl": "/list/a646c12-1.html", "Title": "購寶馬7系價格暫無優惠", "Url": "//www.autohome.com.cn/dealer/201906/366588080.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "途觀L價格直降7.6萬元", "Url": "//www.autohome.com.cn/dealer/201906/366568937.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "購凱迪拉克XTS降8萬", "Url": "//www.autohome.com.cn/dealer/201906/366500646.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "漢蘭達可試駕購車無優惠", "Url": "//www.autohome.com.cn/dealer/201906/366384207.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "寶馬M4價格穩定無優惠", "Url": "//www.autohome.com.cn/dealer/201906/366156789.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "奧迪A8促銷直降26.33萬元", "Url": "//www.autohome.com.cn/dealer/201906/366925378.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "英菲尼迪Q50L可降6.3萬", "Url": "//www.autohome.com.cn/dealer/201906/366863516.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "帝豪新能源價格降8.25萬", "Url": "//www.autohome.com.cn/dealer/201906/366877669.html" }, { "ItemName": "預約", "ItemUrl": "/list/a646c14-1.html", "Title": "撼路者在售現鉅惠5萬", "Url": "//www.autohome.com.cn/dealer/201906/366912121.html" } ] }
提取Result對應的數組
JSONArray jsonArray= jsonObject.getJSONArray("Result");
四、JSONArray提取爲JSONObject
eg:
JSONObject jsonObject = jsonArray.getJSONObject(0);
五、JSONObject獲取value
一、object.getString("key")
二、object.get("key")
六、獲取JSONObject的ket value
JSONArray dateArr = new JSONArray(); Set<String> key = dateArr .keySet(); for (String keyObj:key) { JSONArray hisData = history.getJSONArray(keyObj); }
七、遍歷JSONArray
第一種for循環 JSONArray seriesArr = new JSONArray(); for(int i=0;i<seriesArr .size();i++){ JSONObject object = eggsArr.getJSONObject(i); } 第二種for加強 JSONArray pzListArr = new JSONArray(); for (Object obj:pzListArr) { JSONObject dataObj = JSONObject.parseObject(obj.toString()); }
八、
Map<String, Object> paraMap = new HashMap<String, Object>();
JSONObject.toJSONString(paraMap)
自動過濾參數爲null的數值
八、javaBean轉爲JSONObject
未完待續······