JSON uses Ali's fastJson as a dependency package
The gradle dependency management is as follows:
compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41'
1. String to JSONObject
Preface: String is a string in JSONObject format
eg:
JSONObject jSONObject = JSONObject.parseObject(String);
2. String to JSONArray
Preface: String is a string in JSONArray format
eg:
JSONArray jsonArray= JSONArray.parseArray(String);
3. The array in JSONObject is extracted as 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"
}
]
}
Extract the array corresponding to Result
JSONArray jsonArray= jsonObject.getJSONArray("Result");
4. Extract JSONArray to JSONObject
eg:
JSONObject jsonObject = jsonArray.getJSONObject(0);
5. Get value from JSONObject
1、object.getString("key")
2、object.get("key")
6. Get the ket value of JSONObject
JSONArray dateArr = new JSONArray();
Set<String> key = dateArr .keySet();
for (String keyObj:key) {
JSONArray hisData = history.getJSONArray(keyObj);
}
7. Traverse 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 is converted to a string in JSON format
Map<String, Object> paraMap = new HashMap<String, Object>();
JSONObject.toJSONString(paraMap)
Automatically filter values whose parameters are null
result:
8. Convert javaBean to JSONObject
JSONObject.parseObject(JSONObject.toJSON(javaBean对象).toString());
9. List<entity class> to String
import com.alibaba.fastjson.JSONObject;
List<实体类> value1 = 。。。。。。
JSONObject.toJSONString(value1 )10、JSONArray转List<实体类>
It depends on which one you are happy with. The difference between object and array is not carefully studied.
10. JSONArray to List<entity class>
import com.alibaba.fastjson.JSONArray;
JSONArray objects = JSONArray.parseArray(categoryConstantInfoFromRedis);
List<实体类> categoryConstantInfos = objects.toJavaList(实体类名.class);
众里寻他千百度!!!toJavaList
11. jsonObject to map
JSONObject user = resJson.getJSONObject("user");
Map<String, Object> userMap = new HashMap<>();
//循环转换
for (Map.Entry<String, Object> entry : user.entrySet()) {
userMap.put(entry.getKey(), entry.getValue());
}
System.out.println("map对象:" + userMap.toString());
12. map to jsonObject
//map对象
Map<String, Object> data =new HashMap<>();
String x =JSONObject.toJSONString(data);
System.out.println("json字符串:"+x);