前言:最近需要實現的任務是:寫若干個接口,並且接口中的請求數據是json格式,然后按照請求參數讀取前端提前整理好的json數據,並且將json數據返回到服務器端。
- 主要的工具:Gson 2.8.2
- 項目支撐:springboot
- maven
0、前導——了解一下基本的json語法
JSON是一種類似 XML的語言,是用了存儲和交換文本信息的語法。它的全稱為JavaScript Object Notation(JavaScript 對象表示法)。與xml對比,它更小、更快,更易解析。
想要更好的解析JSON,必須能看懂JSON數據,這樣就必須了解JSON語法,好在它的語法非常簡單,規則如下:
JSON 語法是 JavaScript 對象表示法語法的子集。 - 數據在名稱/值對中:如 "firstName" : "John" - 數據由逗號分隔 - 花括號保存對象 - 方括號保存數組
而組成JSON的值可以為以下幾種:
- 數字(整數或浮點數) - 字符串(在雙引號中) - 邏輯值(true 或 false) - 數組(在方括號中) - 對象(在花括號中) - null
1、導入Gson jar包
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>
關於Gson的詳細信息可以參考:JAVA解析JSON數據
Gson 是 Google 提供的用來在 Java 對象和 JSON 數據之間進行映射的 Java 類庫。可以將一個 JSON 字符串轉成一個 Java 對象,或者反過來。
這是關於GSON的介紹,相對於傳統的json解析,使用GSON能夠極大的簡化了解析流程。
2、下面提供一小段提前整理好的json數據
[ { "zone_id": 100001, "title": "圍欄1", "zone_geometry": { "type": "polygon", "apex": [ { "lng": "113.166096", "lat": "31.727309" }, { "lng": "113.222498", "lat": "31.689881" } ] } }, { "zone_id": 100002, "title": "圍欄2", "zone_geometry": { "type": "polygon", "apex": [ { "lng": "113.462342", "lat": "31.626034" }, { "lng": "113.472525", "lat": "31.538983" } ] } }, ]
分析原始的json數據格式:原始的json數據整體是一個JsonArray,其次是JsonObject,內部包含有很多字段,里面再套有一層JsonObject,再往里面是JsonArray。
通常而言,遇到了一個[ ] 可以定義一個List,碰見一個{ } 可以定義一個實體類。
因此,我這里定義了三個實體類:
從外層往內層依次是:HerdCamera ZoneGeometry Apex
public class HerdCamera { private String zone_id; private String title; private ZoneGeometry zoneGeometry; public String getZone_id() { return zone_id; } public void setZone_id(String zone_id) { this.zone_id = zone_id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public ZoneGeometry getZoneGeometry() { return zoneGeometry; } public void setZoneGeometry(ZoneGeometry zoneGeometry) { this.zoneGeometry = zoneGeometry; } }
import java.util.List; public class ZoneGeometry { private String type; private List<Locations> apex; public String getType() { return type; } public void setType(String type) { this.type = type; } public List<Locations> getApex() { return apex; } public void setApex(List<Locations> apex) { this.apex = apex; } }
public class Apex { private double lng; private double lat; public Apex(double lng, double lat) { super(); this.lng = lng; this.lat = lat; } public double getLng() { return lng; } public void setLng(double lng) { this.lng = lng; } public double getLat() { return lat; } public void setLat(double lat) { this.lat = lat; } }
3、開始依次從外往內部解析數據源
public Map<String,Object> herdcameradata() throws Exception{ String fileName = "fileName"; JsonParser parser = new JsonParser(); // 創建JSON解析器 JsonArray array = (JsonArray) parser.parse(new FileReader(fileName)); // 首先解析出來的是JsonArray Map< String, Object> result=new HashMap<>(); List<Object> herdCameras = new ArrayList<Object>(); for (int i = 0; i < array.size(); i++) { JsonObject subObject = array.get(i).getAsJsonObject(); // 第二步獲得的是JsonObject HerdCamera herd = new HerdCamera(); herd.setZone_id(subObject.get("zone_id").getAsString()); //然后依次獲取subObject中的每一個字段 herd.setTitle(subObject.get("title").getAsString()); ZoneGeometry zoneGeometry = new ZoneGeometry(); JsonObject subObject2 = subObject.getAsJsonObject("zone_geometry"); //第四步又獲得一個zone_geometry的JsonObject zoneGeometry.setType(subObject2.get("type").getAsString()); //然后獲取zone_geometry內部的type對象 JsonArray array2 = subObject2.getAsJsonArray("apex"); //第五步中apex是一個jsonArray List<Locations> locationList = new ArrayList<Locations>(); for (int j = 0; j < array2.size(); j++) { Locations location = new Locations(); JsonObject subObject3 = array2.get(j).getAsJsonObject(); location.setLng(subObject3.get("lng").getAsString()); location.setLat(subObject3.get("lat").getAsString()); locationList.add(location); } zoneGeometry.setApex(locationList); herd.setZoneGeometry(zoneGeometry); herdCameras.add(herd); } result.put("cameras",herdCameras ); return result; }
4、結束
到現在,所有的解析都已經基本完成,但配合着controller中的映射就可以在前端獲取到相關的數據。
下面給出我獲取到的情況: