数据格式如下,写个对象吧你看看这个数据格式,真麻烦!写个List吧,也是一层一层的!所以决定用map来收取
{"noticeType": "UNIFORM_SUBSCRIPTION",
"notice": { "returnCode": "000000",
"description": "&½",
"orderId": "74f8ce8913d74969a725ef7c84f64b4b",
"productContent": { "orderType": "1",
"productId": "9020500020090429000894",
"callNumber": "xxxxxxxxxxx",
"productPkgId": "00",
"contentId": "00",
"productName": "7000100301"}}}
这是notice里面的数据的实体类
具体代码如下
public static void main(String[] args) {
String str="{\"noticeType\": \"UNIFORM_SUBSCRIPTION\"," +
" \"notice\": {" +
" \"returnCode\": \"000000\"," +
" \"description\": \"&½\"," +
" \"orderId\": \"74f8ce8913d74969a725ef7c84f64b4b\"," +
" \"productContent\": {" +
" \"orderType\": \"1\"," +
" \"productId\": \"9020500020090429000894\"," +
" \"callNumber\": \"xxxxxxxxxxx\"," +
" \"productPkgId\": \"00\"," +
" \"contentId\": \"00\"," +
" \"productName\": \"7000100301\"}}}";
//打印json数据
System.out.println(str);
//打印结果为{"noticeType": "UNIFORM_SUBSCRIPTION", "notice": { "returnCode": "000000", "description": "&½", "orderId": "74f8ce8913d74969a725ef7c84f64b4b", "productContent": { "orderType": "1", "productId": "9020500020090429000894", "callNumber": "xxxxxxxxxxx", "productPkgId": "00", "contentId": "00", "productName": "7000100301"}}}
//把json数据放到map里
Map maps = (Map)JSON.parse(str);
//打印notice部分的value
//打印结果为{"returnCode":"000000","orderId":"74f8ce8913d74969a725ef7c84f64b4b","productContent":{"orderType":"1","productId":"9020500020090429000894","callNumber":"xxxxxxxxxxx","contentId":"00","productPkgId":"00","productName":"7000100301"},"description":"&½"}
System.out.println(maps.get("notice"));
//取notice部分的json数据塞到实体里
String str2=maps.get("notice").toString();
CommitReceiveResult ls = JSONObject.parseObject(str2, CommitReceiveResult.class);
//打印结果为xxxxxxxxxxx
//74f8ce8913d74969a725ef7c84f64b4b
System.out.println(ls.getProductContent().getCallNumber());
System.out.println(ls.getOrderId());
System.out.println("=======================");
//这里用的是蠢办法,一段一段的去取值
Map maps2 = (Map)JSON.parse(str2);
System.out.println(maps2.get("productContent"));
String str3=maps2.get("productContent").toString();
Map maps3 = (Map)JSON.parse(str3);
System.out.println(maps3.get("productId"));
System.out.println("------------------------");
//这个就是直接map嵌map,可以不用一段一段的去取,直接.get().get()就能取到相应的值
Map
System.out.println(map.get("notice").toString());
System.out.println(map.get("notice").get("productContent"));
System.out.println(map.get("notice").get("returnCode"));
System.out.println(map.get("notice").get("productContent").get("orderType"));
System.out.println(map.get("notice").get("productContent").get("productId"));
}
如果大家有更好更快的方法欢迎大家指导,谢谢!