日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java object数组转实体类_详解Java中String JSONObject JSONArray List实体类转换

發布時間:2025/3/12 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java object数组转实体类_详解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 key = dateArr .keySet();

for (String keyObj:key) {

JSONArray hisData = history.getJSONArray(keyObj);

}

7、遍歷JSONArray

第一種for循環

JSONArray seriesArr = new JSONArray();

for(int i=0;i

JSONObject object = eggsArr.getJSONObject(i);

}

第二種for增強

JSONArray pzListArr = new JSONArray();

for (Object obj:pzListArr) {

JSONObject dataObj = JSONObject.parseObject(obj.toString());

}

8、

Map paraMap = new HashMap();

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轉換 內容請搜索云海天教程以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持云海天教程!

原文鏈接:https://www.cnblogs.com/ljangle/p/11047111.html

總結

以上是生活随笔為你收集整理的java object数组转实体类_详解Java中String JSONObject JSONArray List实体类转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。