有时候用json时会忘了怎么转,记一下
原文链接:
https://www.cnblogs.com/cdf-opensource-007/p/7106018.html
https://www.cnblogs.com/ljangle/p/11047111.html
https://blog.csdn.net/weixin_33446857/article/details/78363914
一.jar包
commons-beanutils-1.7.0
commons-collections-3.1
commons-lang-2.5
commons-logging
ezmorph-1.0.3
json-lib-2.1-jdk15
下载请转:
https://blog.csdn.net/bingocoder/article/details/89191847
二.JSONObject JSONArray 转换
1.将符合json格式的字符串转为json对象并分别取出里面的数据。
package dataStructure;
import net.sf.json.JSONObject;
public class json {
/*
* 将符合json格式的字符串转为json对象并分别取出里面的数据。
*/
public static void main(String[] args) {
//模拟获取到的符合json格式的字符串{"data":{"pow":100,"net":99,"dev":69},"success":true,"message":"成功"}
String jsonStr="{\"data\":{\"pow\":100,\"net\":99,\"dev\":69},\"success\":true,\"message\":\"成功\"}";
System.out.println("1-将符合json格式的字符串jsonStr "+jsonStr);
//将符合json格式的字符串转换为JSONObject
JSONObject jsonObject=JSONObject.fromObject(jsonStr);
System.out.println("2-转换为JSONObject "+jsonObject);
//取出json对象里的data数据。
JSONObject jsonData=jsonObject.getJSONObject("data");
System.out.println("3-取出json对象里的data数据 jsonData "+jsonData);
String success=jsonObject.getString("success");
String message=jsonObject.getString("message");
System.out.println("4-success "+success+" | message "+message);
//取出jsonData数据里的pow,net,dev数据。
String pow=jsonData.getString("pow");
String net=jsonData.getString("net");
String dev=jsonData.getString("dev");
System.out.println("5-pow "+pow+" | "+"net "+net+" | "+"dev "+dev);
}
}
结果:
2.将符合JSONArray格式的字符串数组转为json对象数组并分别取出里面的数据。
package dataStructure;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class json1 {
/*
* 将符合JSONArray格式的字符串数组转为json对象数组并分别取出里面的数据。
* 引入json jar包时,请引入"import net.sf.json.JSONObject"
*/
public static void main(String[] args) {
//模拟获取到的json对象数组格式的字符串{"total":"1","rows":[{"createdate":"2017-10-26 00:00:00","name":"aaaa"},{"createdate":"2017-10-27 13:39:35","name":"bbb"}]}
String jsonArrayStr="{\"total\":\"1\",\"rows\":[{\"createdate\":\"2017-10-26 00:00:00\",\"name\":\"aaaa\"},{\"createdate\":\"2017-10-27 13:39:35\",\"name\":\"bbb\"}]}";
System.out.println("1-将符合json对象数组格式的字符串jsonArrayStr "+jsonArrayStr);
//将符合json对象数组格式的字符串转换为json对象
JSONObject jsonObject=JSONObject.fromObject(jsonArrayStr);
System.out.println("2-转换为JSONObject "+jsonObject);
//取出json对象里的total数据。
String strTotal=jsonObject.getString("total");
System.out.println("3-取出json对象里的total数据 total "+strTotal);
//取出json对象里的rows数据
String strRows=jsonObject.getString("rows");
System.out.println("4-取出json对象里的rows数据 jsonRows "+strRows);
//转换成json对象数组
JSONArray jsonArrayRows= JSONArray.fromObject(strRows);
//从数组里取出各值。
for(int i=0;i<jsonArrayRows.size();i++) {
JSONObject jsonObjectRows=(JSONObject)jsonArrayRows.get(i);
String createdate=jsonObjectRows.get("createdate").toString();
String name=jsonObjectRows.get("name").toString();
System.out.println("5-createdate "+createdate+" | name "+name);
}
}
}
结果:
三. String JSONObject JSONArray 转换
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"
}
]
}
提取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.自动过滤参数为null的值
Map<String, Object> paraMap = new HashMap<String, Object>();
JSONObject.toJSONString(paraMap)
//自动过滤参数为null的数值