JSON使用阿裡的fastJson為依賴包
gradle依賴管理如下:
compile group: \'com.alibaba\', name: \'fastjson\', version:\'1.2.41\'
1、String轉JSONObject
前言:String 是JSONObject格式的字元串
eg:

JSONObject jSONObject = JSONObject.parseObject(String);
2、String轉JSONArray
前言:String 是JSONArray格式的字元串
eg:
JSONArray jsonArray= JSONArray.parseArray(String);
3、JSONObject中的數組提取為JSONArray
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");
4、JSONArray提取為JSONObject
eg:
JSONObject jsonObject = jsonArray.getJSONObject(0);
5、JSONObject擷取value
1、object.getString("key")
2、object.get("key")
6、擷取JSONObject的ket value
JSONArray dateArr = new JSONArray();
Set<String> key = dateArr .keySet();
for (String keyObj:key) {
JSONArray hisData = history.getJSONArray(keyObj);
}
7、周遊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());
}
8、Map轉為JSON格式的字元串
Map<String, Object> paraMap = new HashMap<String, Object>();
JSONObject.toJSONString(paraMap)
自動過濾參數為null的數值
結果:
8、javaBean轉為JSONObject
JSONObject.parseObject(JSONObject.toJSON(javaBean對象).toString());
9、List<實體類>轉String
import com.alibaba.fastjson.JSONObject;
List<實體類> value1 = 。。。。。。
JSONObject.toJSONString(value1 )10、JSONArray轉List<實體類>
看你開心用哪個,object和array的差別沒有細究
10、JSONArray轉List<實體類>
import com.alibaba.fastjson.JSONArray;
JSONArray objects = JSONArray.parseArray(categoryConstantInfoFromRedis);
List<實體類> categoryConstantInfos = objects.toJavaList(實體類名.class);
衆裡尋他千百度!!!toJavaList
找不到方法的時候,去看看JSONArray,JSONObject的源碼,很多都有封裝好的,你不會失望的