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的源码,很多都有封装好的,你不会失望的
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。





