alibabajison处理
2017-11-02 本文已影响0人
陈大水牛
pom文件:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
json case:
//日期转换 :
String name=JSON.toJSONStringWithDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
System.out.println(name);
//输出"2018-05-08 18:08:39"
//String 转换json,json转换为String:
String name="{\"bankCode\":\"CMB\",\"amount\":\"1.00\",\"orderNo\":" +
"\"201804251409213988\",\"merchantId\":\"200000000004381\",\"token\":" +
"\"C214FD0B7B6D3F912408B2F215127638\",\"channelAppType\":\"BANKPAY\"}";
JSONObject jsonObject =JSONObject.parseObject(name);
String str = JSON.toJSONString(jsonObject );
System.out.println(str);
//map转换成String的json串
Map m1= new HashMap();
m1.put("name","chenliuca");m1.put("home","china");
String str = JSON.toJSONString(m1);
System.out.println(str);
//json转换为map或者其他对象
(Map) JSON.parseObject(str)
//对象转换为jsonString
Happiness happ =new Happiness();
happ.setCity("北京");happ.setId(10L);happ.setNum(12);
String str = JSON.toJSONString(happ);
System.out.println(str);
//输出{"city":"北京","id":10,"num":12}
根据下列获取取对应json内容
JSONObject jsonObject=JSONObject.parseObject(name);
//判断是是不是json格式
/* public boolean isJson(String name){
if(name.equals("")){
return false;
}
try {
JSONObject jsonStr= JSONObject.parseObject(name);
return true;
} catch (Exception e) {
return false;
}
}*/
String str = JSON.toJSONString(jsonObject);
System.out.println(str);
System.out.println("totalRecords为 :"+jsonObject.getString("totalRecords"));
System.out.println("result: "+jsonObject.getString("result"));
System.out.println("result: "+jsonObject.getJSONObject("result"));
System.out.println("selList :"+jsonObject.getJSONArray("selList"));
System.out.println("selList第一个: "+jsonObject.getJSONArray("selList").get(0));
System.out.println("获取JSONArray里面第一个json :"+jsonObject.getJSONArray("selList").getJSONObject(0));
System.out.println("regNO为: "+jsonObject.getJSONArray("selList").getJSONObject(0).getString("regNO"));
System.out.println("判断是不是json类型 :"+jsonObject.getJSONArray("selList").getJSONObject(0).getString("regNO"));
/*
{"result":{"code":"200","status":"success"},"totalRecords":2615,"selList":[{"regNO":"442000602187424","abnTime":"Mar 13, 2015 12:00:00 AM","entNameUrl":"<a href=\"..\">","entNo":"16da9629-0131-1000-e005-3effc0a803a8"},{"decOrg":"442020","auditingFileNo":"15000684990326","abnormalID":"fd74013d-014b-1000-e00a-72970a0d0114","entNameUrl":"<a href=\".. "}],"pageNo":1,"totalPages":327,"pageSize":8,"list":[{"regNO":"442000600169663","abnTime":"Mar 13, 2015 12:00:00 AM","entNameUrl":"<a href=\".. ","entNo":"1c2e4ca8-00fa-1000-e000-74590a76bf0f"},{"decOrg":"442020","auditingFileNo":"15000684990326","abnormalID":"fd74013d-014b-1000-e00a-72970a0d0114","entNameUrl":"<a href=\".. "}],"topPageNo":1,"url":"main/abnInfoPage"}
totalRecords为 :2615
result: {"code":"200","status":"success"}
result: {"code":"200","status":"success"}
selList :[{"regNO":"442000602187424","abnTime":"Mar 13, 2015 12:00:00 AM","entNameUrl":"<a href=\"..\">","entNo":"16da9629-0131-1000-e005-3effc0a803a8"},{"decOrg":"442020","auditingFileNo":"15000684990326","abnormalID":"fd74013d-014b-1000-e00a-72970a0d0114","entNameUrl":"<a href=\".. "}]
selList第一个: {"regNO":"442000602187424","abnTime":"Mar 13, 2015 12:00:00 AM","entNameUrl":"<a href=\"..\">","entNo":"16da9629-0131-1000-e005-3effc0a803a8"}
获取JSONArray里面第一个json :{"regNO":"442000602187424","abnTime":"Mar 13, 2015 12:00:00 AM","entNameUrl":"<a href=\"..\">","entNo":"16da9629-0131-1000-e005-3effc0a803a8"}
regNO为: 442000602187424
判断是不是json类型 :442000602187424
*/
{
"totalRecords": 2615,
"result": {
"code": "200",
"status": "success"
},
"list": [{
"entNo": "1c2e4ca8-00fa-1000-e000-74590a76bf0f",
"regNO": "442000600169663",
"abnTime": "Mar 13, 2015 12:00:00 AM",
"entNameUrl": "<a href=\".. ",
}, {
"decOrg": "442020",
"entNameUrl": "<a href=\".. ",
"auditingFileNo": "15000684990326",
"abnormalID": "fd74013d-014b-1000-e00a-72970a0d0114"
}],
"pageNo": 1,
"pageSize": 8,
"url": "main/abnInfoPage",
"selList": [{
"entNo": "16da9629-0131-1000-e005-3effc0a803a8",
"regNO": "442000602187424",
"abnTime": "Mar 13, 2015 12:00:00 AM",
"entNameUrl": "<a href=\"..\">",
}, {
"decOrg": "442020",
"entNameUrl": "<a href=\".. ",
"auditingFileNo": "15000684990326",
"abnormalID": "fd74013d-014b-1000-e00a-72970a0d0114"
}],
"topPageNo": 1,
"totalPages": 327
}