The task that needs to be accomplished recently is to write several interfaces, and the request data in the interface is in JSON format, then reads the JSON data in advance by the request parameters and returns the JSON data to the server side.
- The main tool: Gson 2.8.2
- Project Support: Springboot
- Maven
0. Preamble--Learn basic JSON syntax
JSON is an XML-like language that uses the syntax for storing and exchanging textual information. It's all called JavaScript Object Notation (JavaScript objects notation). Compared to XML, it is smaller, faster, and easier to parse.
To better parse the JSON, you must be able to understand the JSON data, so you must understand the JSON syntax, but the syntax is very simple, the rules are as follows:
-Data in name/value pairs: such as "FirstName": "John"---square brackets to save the array
The values that make up the JSON can be in the following ways:
- number (integer or floating point)- string (in double quotes)-logical value (truefalse)- Array (in square brackets)- Object (in curly braces)null
1. Import Gson jar Package
<!--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>
For more information on Gson, refer to: Java parsing JSON data
Gson is a Java class library provided by Google for mapping between Java objects and JSON data. You can turn a JSON string into a Java object, or vice versa.
This is an introduction to Gson, and the use of Gson can greatly simplify the parsing process compared to the traditional JSON parsing.
2, the following provides a small piece of pre-organized JSON data
[ { "zone_id": 100001, "title": "Fence 1", "Zone_geometry": { "Type": "Polygon", "Apex": [ { "LNG": "113.166096", "Lat": "31.727309" }, { "LNG": "113.222498", "Lat": "31.689881" } ] } }, { "zone_id": 100002, "title": "Fence 2", "Zone_geometry": { "Type": "Polygon", "Apex": [ { "LNG": "113.462342", "Lat": "31.626034" }, { "LNG": "113.472525", "Lat": "31.538983" } ] } },]
parse the original JSON data format: The original JSON data is a jsonarray, followed by a jsonobject, the interior contains a lot of fields, inside there is a layer of jsonobject, and then jsonarray inside.
In general, encountering a [] can define a list, encounter a {} can define an entity class.
Therefore, I have defined three entity classes here:
From the outer layers to the inner layer are:herdcamera zonegeometry Apex
Public classHerdcamera {PrivateString zone_id; PrivateString title; PrivateZonegeometry Zonegeometry; PublicString getzone_id () {returnzone_id; } Public voidsetzone_id (String zone_id) { This. zone_id =zone_id; } PublicString GetTitle () {returntitle; } Public voidSettitle (String title) { This. title =title; } Publiczonegeometry getzonegeometry () {returnZonegeometry; } Public voidsetzonegeometry (Zonegeometry zonegeometry) { This. Zonegeometry =Zonegeometry; }}
Importjava.util.List; Public classZonegeometry {PrivateString type; PrivateList<locations>Apex; PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; } PublicList<locations>Getapex () {returnApex; } Public voidSetapex (list<locations>Apex) { This. Apex =Apex; }}
Public classApex {Private DoubleLNG; Private Doublelat; PublicApex (DoubleLngDoublelat) { Super(); This. LNG =LNG; This. lat =lat; } Public Doublegetlng () {returnLNG; } Public voidSETLNG (DoubleLNG) { This. LNG =LNG; } Public DoubleGetlat () {returnlat; } Public voidSetlat (Doublelat) { This. lat =lat; }}
3. Start parsing data source from outside to inside
PublicMap<string,object> Herdcameradata ()throwsexception{String fileName= "FileName"; Jsonparser Parser=NewJsonparser ();//creating a JSON parserJsonarray array = (Jsonarray) parser.parse (NewFileReader (FileName));//the first thing to parse out is Jsonarray.Map< String, object> result=NewHashmap<>(); List<Object> Herdcameras =NewArraylist<object>(); for(inti = 0; I < array.size (); i++) {Jsonobject subobject= Array.get (i). Getasjsonobject ();//The second step was to get jsonobject.Herdcamera herd =NewHerdcamera (); HERD.SETZONE_ID (Subobject.get ("zone_id"). getasstring ());//then get each of the fields in SubobjectHerd.settitle (Subobject.get ("title"). getasstring ()); Zonegeometry Zonegeometry=NewZonegeometry (); Jsonobject SubObject2= Subobject.getasjsonobject ("Zone_geometry");//the fourth step and get a zone_geometry jsonobjectZonegeometry.settype (Subobject2.get ("type"). getasstring ());//then get the type object inside the Zone_geometryJsonarray array2 = Subobject2.getasjsonarray ("Apex");//in the fifth step, Apex is a jsonarrayList<locations> locationlist =NewArraylist<locations>(); for(intj = 0; J < Array2.size (); J + +) {Locations location=NewLocations (); 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); returnresult; }
4. End
By now, all of the parsing has been done basically, but with the mapping in the controller you can get the relevant data in the front end.
Here's what I get:
How to parse a JSON string and return JSON data to the front end