JAVA——利用Map集合,再複雜的JSON資料轉換都不慌!
在我們悲催的碼農開發過程中百分百會接觸到Json資料,並且有時候還需要把JSON資料轉換成JAVA物件,簡單的JSON資料轉換成JAVA物件很簡單,簡單的JSON資料可能只需要兩個JAVA類,一個接收最外層的資料,一個接收集合,但是對於複雜的JSON,有很多層而且不規則,那麼再用這種方法一個個轉就很愚蠢了!
這個時候就用Map集合來操作它就行,我相信都不需要我說的很清楚你就可以一點就通~就拿我最近碰到的一個案例來說~
最近在專案中接入順豐API來實時查詢物流資訊,查詢出來的JSON資料相對於普通的要稍微複雜一點點點點點,就拿它舉個例子
{ "Response": { "Head": "OK", "service": "RouteService", "Body": { "RouteResponse": { "mailno": 444000092295, "orderid": "QIAO-20180104001", "Route": [{ "accept_address": "廣東省深圳市軟體產業基地", "remark": "順豐速運 已收取快件(測試資料)", "opcode": 50, "accept_time": "2018-05-01 08:01:44" }, { "accept_address": "廣東省深圳市軟體產業基地", "remark": "已簽收,感謝使用順豐,期待再次為您服務(測試資料)", "opcode": 80, "accept_time": "2018-05-02 12:01:44" }] } } } }
在這段JSON資料中,其實對於我來說有用的只是“Route”下對應的資料,我現在想把它轉換成這樣的一個JAVA物件
public class ShunFengVo { private String accept_address; private String remark; private Integer opcode; private String accept_time; public String getAccept_address() { return accept_address; } public void setAccept_address(String accept_address) { this.accept_address = accept_address; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public Integer getOpcode() { return opcode; } public void setOpcode(Integer opcode) { this.opcode = opcode; } public String getAccept_time() { return accept_time; } public void setAccept_time(String accept_time) { this.accept_time = accept_time; } }
好方便我去前端頁面遍歷顯示資料,也許還需要 “mailno”這個欄位,因為他是物流運單號,當然這也是小問題啦
接下來看我操作
package com.sixmac.platform.model.vo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.sixmac.core.annotation.Json; import com.sixmac.core.toolbox.kit.JsonKit; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; import java.util.Map; /** * { * "Response": { * "Head": "OK", * "service": "RouteService", * "Body": { * "RouteResponse": { * "mailno": 444000092295, * "orderid": "QIAO-20180104001", * "Route": [{ * "accept_address": "廣東省深圳市軟體產業基地", * "remark": "順豐速運 已收取快件(測試資料)", * "opcode": 50, * "accept_time": "2018-05-01 08:01:44" * }, { * "accept_address": "廣東省深圳市軟體產業基地", * "remark": "已簽收,感謝使用順豐,期待再次為您服務(測試資料)", * "opcode": 80, * "accept_time": "2018-05-02 12:01:44" * }]* }* } * } * } */ public class ShunFengModel { public static void main(String[] args) { String s="{\n" + "\t\"Response\": {\n" + "\t\t\"Head\": \"OK\",\n" + "\t\t\"service\": \"RouteService\",\n" + "\t\t\"Body\": {\n" + "\t\t\t\"RouteResponse\": {\n" + "\t\t\t\t\"mailno\": 444000092295,\n" + "\t\t\t\t\"orderid\": \"QIAO-20180104001\",\n" + "\t\t\t\t\"Route\": [{\n" + "\t\t\t\t\t\"accept_address\": \"廣東省深圳市軟體產業基地\",\n" + "\t\t\t\t\t\"remark\": \"順豐速運 已收取快件(測試資料)\",\n" + "\t\t\t\t\t\"opcode\": 50,\n" + "\t\t\t\t\t\"accept_time\": \"2018-05-01 08:01:44\"\n" + "\t\t\t\t}, {\n" + "\t\t\t\t\t\"accept_address\": \"廣東省深圳市軟體產業基地\",\n" + "\t\t\t\t\t\"remark\": \"已簽收,感謝使用順豐,期待再次為您服務(測試資料)\",\n" + "\t\t\t\t\t\"opcode\": 80,\n" + "\t\t\t\t\t\"accept_time\": \"2018-05-02 12:01:44\"\n" + "\t\t\t\t}]\n" + "\t\t\t}\n" + "\t\t}\n" + "\t}\n" + "}"; Map map4 = JSON.parseObject(s, Map.class); System.out.println(map4.toString()); } }
s就是那串JSON,首先通過JSON提供的方法轉行成一個Map集合轉然後toString,這個時候,"Response"就相當於是key,後面的資料就是value
打印出來的結果
{Response={"Head":"OK","service":"RouteService","Body":{"RouteResponse":{"mailno":444000092295,"orderid":"QIAO-20180104001","Route":[{"accept_address":"廣東省深圳市軟體產業基地","remark":"順豐速運 已收取快件(測試資料)","opcode":50,"accept_time":"2018-05-01 08:01:44"},{"accept_address":"廣東省深圳市軟體產業基地","remark":"已簽收,感謝使用順豐,期待再次為您服務(測試資料)","opcode":80,"accept_time":"2018-05-02 12:01:44"}]}}}}
這個時候我們只需要這個“Response”的value,就直接取出value然後轉成String就行
String s1 = map.get("Response").toString();
這個時候打印出來的資料是這樣的
{"Head":"OK","service":"RouteService","Body":{"RouteResponse":{"mailno":444000092295,"orderid":"QIAO-20180104001","Route":[{"accept_address":"廣東省深圳市軟體產業基地","remark":"順豐速運 已收取快件(測試資料)","opcode":50,"accept_time":"2018-05-01 08:01:44"},{"accept_address":"廣東省深圳市軟體產業基地","remark":"已簽收,感謝使用順豐,期待再次為您服務(測試資料)","opcode":80,"accept_time":"2018-05-02 12:01:44"}]}}}
然後再把這段資料通過上面一樣的方法轉為Map
Map map1 = JSON.parseObject(s1, Map.class);
轉行為String型別打印出來的資料如下
{Head=OK, service=RouteService, Body={"RouteResponse":{"mailno":444000092295,"orderid":"QIAO-20180104001","Route":[{"accept_address":"廣東省深圳市軟體產業基地","remark":"順豐速運 已收取快件(測試資料)","opcode":50,"accept_time":"2018-05-01 08:01:44"},{"accept_address":"廣東省深圳市軟體產業基地","remark":"已簽收,感謝使用順豐,期待再次為您服務(測試資料)","opcode":80,"accept_time":"2018-05-02 12:01:44"}]}}}
相信到這裡基本上的思路已經都有了,可以看到我們需要的資料在Body中,我們按照上面一模一樣的思路取出Body的value並轉換為String型別!
String s2 = map1.get("Body").toString();
System.out.println(s2);
打印出來的結果如下
{"RouteResponse":{"mailno":444000092295,"orderid":"QIAO-20180104001","Route":[{"accept_address":"廣東省深圳市軟體產業基地","remark":"順豐速運 已收取快件(測試資料)","opcode":50,"accept_time":"2018-05-01 08:01:44"},{"accept_address":"廣東省深圳市軟體產業基地","remark":"已簽收,感謝使用順豐,期待再次為您服務(測試資料)","opcode":80,"accept_time":"2018-05-02 12:01:44"}]}}
可以看到離我們需要的資料越來越近了
然後相信你們都已經會了,只需要繼續重複上面的步驟轉換型別然後取出value即可!
Map map2 = JSON.parseObject(s2, Map.class);
System.out.println(map2.toString());
打印出來的結果
{RouteResponse={"mailno":444000092295,"orderid":"QIAO-20180104001","Route":[{"accept_address":"廣東省深圳市軟體產業基地","remark":"順豐速運 已收取快件(測試資料)","opcode":50,"accept_time":"2018-05-01 08:01:44"},{"accept_address":"廣東省深圳市軟體產業基地","remark":"已簽收,感謝使用順豐,期待再次為您服務(測試資料)","opcode":80,"accept_time":"2018-05-02 12:01:44"}]}}
只需要取出RouteResponse的value轉換為String就OK了
String s3 = map2.get("RouteResponse").toString();
System.out.println(s3);
列印
{"mailno":444000092295,"orderid":"QIAO-20180104001","Route":[{"accept_address":"廣東省深圳市軟體產業基地","remark":"順豐速運 已收取快件(測試資料)","opcode":50,"accept_time":"2018-05-01 08:01:44"},{"accept_address":"廣東省深圳市軟體產業基地","remark":"已簽收,感謝使用順豐,期待再次為您服務(測試資料)","opcode":80,"accept_time":"2018-05-02 12:01:44"}]}
到這裡已經轉換成我們最常見最簡單的JSON格式了,裡面的資料基本上都是我們所需要的,接下來的我相信不用寫,你們也能知道該怎麼做了。
需要注意的是想把 Route中的資料轉換為JAVA物件的時候要用這個方法
List<ShunFengVo> list = JSON.parseArray(route, ShunFengVo.class);
因為它的最外層有一個[ ],轉換的時候會把它當一個數組,所以要用JSON.parseArray()來轉換,不然會報錯!