StringJSONObjectJSONArrayList实体类转换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/market/201904/100223763.html"
},
{
"ItemName": "优惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "马驹桥林肯中⼼年中⼤促",
"Url": "//www.autohome/market/201906/100230932.html"
},
{
"ItemName": "优惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "星越平价销售13.58万元起",
"Url": "//www.autohome/dealer/201906/367011492.html"
},
{
"ItemName": "优惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "哈弗F5限时优惠8000元",
"Url": "//www.autohome/dealer/201906/366897778.html"
},
{
"ItemName": "优惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "购元新能源价格暂⽆优惠",
"Url": "//www.autohome/dealer/201906/366897034.html"
},
{
"ItemName": "优惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "瑞虎3xe冰点价促销中!",
"Url": "//www.autohome/dealer/201906/366889724.html"
},
{
"ItemName": "优惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "购奔奔EV现钜惠5.1万元",
"Url": "//www.autohome/dealer/201906/366843204.html"
},
{
"ItemName": "优惠",
"ItemUrl": "/list/a646c12-1.html",
"Title": "购宝马7系价格暂⽆优惠",
"Url": "//www.autohome/dealer/201906/366588080.html"
},
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "途观L价格直降7.6万元",
"Url": "//www.autohome/dealer/201906/366568937.html"        },
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "购凯迪拉克XTS降8万",
"Url": "//www.autohome/dealer/201906/366500646.html"        },
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "汉兰达可试驾购车⽆优惠",
"Url": "//www.autohome/dealer/201906/366384207.html"        },
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "宝马M4价格稳定⽆优惠",
"Url": "//www.autohome/dealer/201906/366156789.html"        },
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "奥迪A8促销直降26.33万元",
"Url": "//www.autohome/dealer/201906/366925378.html"        },
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "英菲尼迪Q50L可降6.3万",
"Url": "//www.autohome/dealer/201906/366863516.html"        },
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "帝豪新能源价格降8.25万",
"Url": "//www.autohome/dealer/201906/366877669.html"        },
{
"ItemName": "预定",
"ItemUrl": "/list/a646c14-1.html",
"Title": "撼路者在售现钜惠5万",
"Url": "//www.autohome/dealer/201906/366912121.html"        }
]
}
提取Result对应的数组
JSONArray jsonArray= JSONArray("Result");
4、JSONArray提取为JSONObject
eg:
JSONObject jsonObject = JSONObject(0);
5、JSONObject获取value
1、String("key")
2、("key")
6、获取JSONObject的ket value
JSONArray dateArr = new JSONArray();
Set<String> key = dateArr .keySet();
for (String keyObj:key) {
JSONArray hisData = JSONArray(keyObj);
}
7、遍历JSONArray
第⼀种for循环
数组格式字符串转数组
JSONArray seriesArr = new JSONArray();
for(int i=0;i<seriesArr .size();i++){
JSONObject object = JSONObject(i);
}
第⼆种for增强
JSONArray pzListArr = new JSONArray();
for (Object obj:pzListArr) {
JSONObject dataObj = JSONObject.String());                }
8、Map转为JSON格式的字符串
Map<String, Object> paraMap = new HashMap<String, Object>(); JSONString(paraMap)
⾃动过滤参数为null的数值
结果:
8、javaBean转为JSONObject
JSONObject.JSON(javaBean对象).toString());
9、List<;实体类>转String
import com.alibaba.fastjson.JSONObject;
List<;实体类> value1 = 。。。。。。
看你开⼼⽤哪个,object和array的区别没有细究
10、JSONArray转List<;实体类>
import com.alibaba.fastjson.JSONArray;
JSONArray objects = JSONArray.parseArray(categoryConstantInfoFromRedis); List<;实体类> categoryConstantInfos = JavaList(实体类名.class);
众⾥寻他千百度!!!toJavaList
不到⽅法的时候,去看看JSONArray,JSONObject的源码,很多都有封装好的,你不会失望的

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。