詳解Java中String JSONObject JSONArray List<實體類>轉(zhuǎn)換
JSON使用阿里的fastJson為依賴包
gradle依賴管理如下:
compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41'
1、String轉(zhuǎn)JSONObject
前言:String 是JSONObject格式的字符串
eg:
JSONObject jSONObject = JSONObject.parseObject(String);
2、String轉(zhuǎn)JSONArray
前言:String 是JSONArray格式的字符串
eg:
JSONArray jsonArray= JSONArray.parseArray(String);
3、JSONObject中的數(shù)組提取為JSONArray
eg:
{
"AreaName": "北京",
"CityId": 110100,
"NoMarket": false,
"OldCityId": 646,
"Pinyin": "beijing",
"ProvinceId": 110000,
"Result": [
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "Stelvio 鉅惠23.4萬起",
"Url": "http://www.autohome.com.cn/market/201904/100223763.html"
},
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "馬駒橋林肯中心年中大促",
"Url": "http://www.autohome.com.cn/market/201906/100230932.html"
},
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "星越平價銷售13.58萬元起",
"Url": "http://www.autohome.com.cn/dealer/201906/367011492.html"
},
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "哈弗F5限時優(yōu)惠8000元",
"Url": "http://www.autohome.com.cn/dealer/201906/366897778.html"
},
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "購元新能源價格暫無優(yōu)惠",
"Url": "http://www.autohome.com.cn/dealer/201906/366897034.html"
},
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "瑞虎3xe冰點價促銷中!",
"Url": "http://www.autohome.com.cn/dealer/201906/366889724.html"
},
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "購奔奔EV現(xiàn)鉅惠5.1萬元",
"Url": "http://www.autohome.com.cn/dealer/201906/366843204.html"
},
{
"ItemName": "優(yōu)惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "購寶馬7系價格暫無優(yōu)惠",
"Url": "http://www.autohome.com.cn/dealer/201906/366588080.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "途觀L價格直降7.6萬元",
"Url": "http://www.autohome.com.cn/dealer/201906/366568937.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "購凱迪拉克XTS降8萬",
"Url": "http://www.autohome.com.cn/dealer/201906/366500646.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "漢蘭達(dá)可試駕購車無優(yōu)惠",
"Url": "http://www.autohome.com.cn/dealer/201906/366384207.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "寶馬M4價格穩(wěn)定無優(yōu)惠",
"Url": "http://www.autohome.com.cn/dealer/201906/366156789.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "奧迪A8促銷直降26.33萬元",
"Url": "http://www.autohome.com.cn/dealer/201906/366925378.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "英菲尼迪Q50L可降6.3萬",
"Url": "http://www.autohome.com.cn/dealer/201906/366863516.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "帝豪新能源價格降8.25萬",
"Url": "http://www.autohome.com.cn/dealer/201906/366877669.html"
},
{
"ItemName": "預(yù)定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "撼路者在售現(xiàn)鉅惠5萬",
"Url": "http://www.autohome.com.cn/dealer/201906/366912121.html"
}
]
}

提取Result對應(yīng)的數(shù)組
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循環(huán)
JSONArray seriesArr = new JSONArray();
for(int i=0;i<seriesArr .size();i++){
JSONObject object = eggsArr.getJSONObject(i);
}
第二種for增強(qiáng)
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)
自動過濾參數(shù)為null的數(shù)值
8、javaBean轉(zhuǎn)為JSONObject
未完待續(xù)······
9、List<實體類>轉(zhuǎn)String
import com.alibaba.fastjson.JSONObject; List<實體類> value1 = 。。。。。。 JSONObject.toJSONString(value1 )10、JSONArray轉(zhuǎn)List<實體類>

看你開心用哪個,object和array的區(qū)別沒有細(xì)究
10、JSONArray轉(zhuǎn)List<實體類>
import com.alibaba.fastjson.JSONArray; JSONArray objects = JSONArray.parseArray(categoryConstantInfoFromRedis); List<實體類> categoryConstantInfos = objects.toJavaList(實體類名.class); 眾里尋他千百度?。?!toJavaList

找不到方法的時候,去看看JSONArray,JSONObject的源碼,很多都有封裝好的,你不會失望的
到此這篇關(guān)于詳解Java中String JSONObject JSONArray List<實體類>轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)String JSONObject JSONArray List<實體類>轉(zhuǎn)換 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例
- java使用JSONObject實例
- JAVA中JSONObject對象和Map對象之間的相互轉(zhuǎn)換
- Java 如何遍歷JsonObject對象
- java的JsonObject對象提取值方法
- Java中如何將String轉(zhuǎn)JSONObject
- java中如何判斷JSONObject是否存在某個Key
- 淺析Java中JSONObject和JSONArray使用
- java 將jsonarray 轉(zhuǎn)化為對應(yīng)鍵值的jsonobject方法
- Java使用JSONObject需要的6個jar包下載地址
- Java中JSONObject與JSONArray的使用區(qū)別詳解
- java 各個JSONObject的區(qū)別小結(jié)
相關(guān)文章
詳解Java中String JSONObject JSONArray List<實體類>轉(zhuǎn)換
這篇文章主要介紹了詳解String JSONObject JSONArray List<實體類>轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談JSON中stringify 函數(shù)、toJosn函數(shù)和parse函數(shù)
這篇文章主要介紹了淺談JSON中stringify 函數(shù)、toJosn函數(shù)和parse函數(shù),需要的朋友可以參考下2015-01-01
比較詳細(xì)的關(guān)于javascript 解析json的代碼
JSON (JavaScript Object Notation)一種簡單的數(shù)據(jù)格式,比xml更輕巧。 JSON 是 JavaScript 原生格式,這意味著在 JavaScript 中處理 JSON 數(shù)據(jù)不需要任何特殊的 API 或工具包。2009-12-12

