java list 循环转json,深入浅析Java中的String JSONObject JSONArray List的转换

深入浅析Java中的String JSONObject JSONArray List的转换

发布时间:2020-11-16 15:15:10

来源:亿速云

阅读:99

作者:Leah

今天就跟大家聊聊有关深入浅析Java中的String JSONObject JSONArray List的转换,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

JSON使用阿里的fastJson为依赖包

gradle依赖管理如下:

compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41'

1、String转JSONObject

前言:String 是JSONObject格式的字符串

eg:

2e662f1e4ed5435892a959b07f6ffa9b.png

JSONObject jSONObject = JSONObject.parseObject(String);

2、String转JSONArray

前言:String 是JSONArray格式的字符串

eg:

84813b0675ba76df97be19dd01d6bebd.png

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"

}

]

}

01cbb2c60c830596e98262fb955d535e.png

提取Result对应的数组

JSONArray jsonArray= jsonObject.getJSONArray("Result");

4、JSONArray提取为JSONObject

eg:

895b96111ab31ca63f2deb208ff0e33a.png

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

1be181195dfc763224c7c558804d49fe.png

看你开心用哪个,object和array的区别没有细究

10、JSONArray转List

import com.alibaba.fastjson.JSONArray;

JSONArray objects = JSONArray.parseArray(categoryConstantInfoFromRedis);

List categoryConstantInfos = objects.toJavaList(实体类名.class);

众里寻他千百度!!!toJavaList

858118fdc8b711891107f26e5615be6d.png

找不到方法的时候,去看看JSONArray,JSONObject的源码,很多都有封装好的,你不会失望的

看完上述内容,你们对深入浅析Java中的String JSONObject JSONArray List的转换有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值