詳解Java中String JSONObject JSONArray List<實體類>轉換
阿新 • • 發佈:2020-11-13
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": "優惠","Title": "馬駒橋林肯中心年中大促","Url": "//www.autohome.com.cn/market/201906/100230932.html" },"Title": "星越平價銷售13.58萬元起","Url": "//www.autohome.com.cn/dealer/201906/367011492.html" },"Title": "哈弗F5限時優惠8000元","Url": "//www.autohome.com.cn/dealer/201906/366897778.html" },"Title": "購元新能源價格暫無優惠","Url": "//www.autohome.com.cn/dealer/201906/366897034.html" },"Title": "瑞虎3xe冰點價促銷中!","Url": "//www.autohome.com.cn/dealer/201906/366889724.html" },"Title": "購奔奔EV現鉅惠5.1萬元","Url": "//www.autohome.com.cn/dealer/201906/366843204.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" },"Title": "購凱迪拉克XTS降8萬","Url": "//www.autohome.com.cn/dealer/201906/366500646.html" },"Title": "漢蘭達可試駕購車無優惠","Url": "//www.autohome.com.cn/dealer/201906/366384207.html" },"Title": "寶馬M4價格穩定無優惠","Url": "//www.autohome.com.cn/dealer/201906/366156789.html" },"Title": "奧迪A8促銷直降26.33萬元","Url": "//www.autohome.com.cn/dealer/201906/366925378.html" },"Title": "英菲尼迪Q50L可降6.3萬","Url": "//www.autohome.com.cn/dealer/201906/366863516.html" },"Title": "帝豪新能源價格降8.25萬","Url": "//www.autohome.com.cn/dealer/201906/366877669.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<String,Object> paraMap = new HashMap<String,Object>(); JSONObject.toJSONString(paraMap)
自動過濾引數為null的數值
8、javaBean轉為JSONObject
未完待續······
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的原始碼,很多都有封裝好的,你不會失望的
到此這篇關於詳解Java中String JSONObject JSONArray List<實體類>轉換的文章就介紹到這了,更多相關String JSONObject JSONArray List<實體類>轉換 內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!