gpt4 book ai didi

详解Java中String JSONObject JSONArray List<实体类>转换

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 28 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章详解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"    }  ]}

详解Java中String JSONObject JSONArray List<实体类>转换

提取Result对应的数组 。

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

4、JSONArray提取为JSONObject 。

eg

详解Java中String JSONObject JSONArray List<实体类>转换

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<实体类>

详解Java中String JSONObject JSONArray List<实体类>转换

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

10、JSONArray转List<实体类> 。

import com.alibaba.fastjson.JSONArray;JSONArray objects = JSONArray.parseArray(categoryConstantInfoFromRedis);List<实体类> categoryConstantInfos = objects.toJavaList(实体类名.class);众里寻他千百度!!!toJavaList

详解Java中String JSONObject JSONArray List<实体类>转换

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

到此这篇关于详解Java中String JSONObject JSONArray List<实体类>转换的文章就介绍到这了,更多相关String JSONObject JSONArray List<实体类>转换 内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

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

最后此篇关于详解Java中String JSONObject JSONArray List<实体类>转换的文章就讲到这里了,如果你想了解更多关于详解Java中String JSONObject JSONArray List<实体类>转换的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com