您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關深入淺析Java中的String JSONObject JSONArray List的轉換,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
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<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的轉換有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。