JSON使用阿里的fastJson为依赖包
gradle依赖管理如下:
compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41'
1、String转JSONObject
前言:String 是JSONObject格式的字符串
JSONObject jSONObject = JSONObject.parseObject(String);
2、String转JSONArray
前言:String 是JSONArray格式的字符串
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的源码,很多都有封装好的,你不会失望的
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 炉石传说月初最强卡组有哪些 2024月初最强上分卡组推荐
- 狼人杀亮相原生鸿蒙之夜 假面科技强势登陆华为生态
- 12小时光线挑战!AI画质专家才是大平层首选
- 2024游戏IP报告:1~9月规模1960亿 68%用户愿为之付费
- 群星.2024-今夜一起为爱鼓掌电视剧原声带【相信音乐】【FLAC分轨】
- BIGFOUR.2013-大家利事【寰亚】【WAV+CUE】
- 李美凤.1992-情深透全情歌集【EMI百代】【WAV+CUE】
- 田震2024-《时光音乐会》[金峰][WAV+CUE]
- 群星《监听天碟3》[LECD]限量版[WAV+CUE]
- 心妤《声如夏花HQ》头版限量编号[WAV+CUE]
- 群星《摇滚五杰》[低速原抓WAV+CUE][1.1G]
- 群星 《2024好听新歌30》十倍音质 U盘音乐 [WAV+分轨]
- 群星《试音草原·女声篇》经典蒙古民歌[WAV+CUE][1G]
- 陈慧娴《永远是你的朋友》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]
- 曼丽·女人三十《如果·爱》限量1:1母盘直刻[低速原抓WAV+CUE]