Complex nested data type JSON format conversion (Geojsonpolygon) using Fastjson in network transport

Source: Internet
Author: User

If an object is too complex, the JSON-formatted data conversion in the network transfer key is prone to problems.

For example, the following class of Area.java

Import Lombok. Allargsconstructor; Import Lombok. Data; Import Lombok. Noargsconstructor; Import Org.springframework.data.mongodb.core.geo.GeoJsonPolygon, @Data @noargsconstructor@allargsconstructor  Public class Area {    private  String name;     Private Geojsonpolygon Geojsonpolygon;}

In this area class, there is a property geojsonploygon a bit complex, the source of this class I do not post, just to show you the area object contains its JSON format data representation:

 { "name": "AAAA"  "geojsonpolygon " : {  "            Span style= "COLOR: #ff0000" > "points": [{"X": 3.4, "Y": 3.9}, {"X": 6.2,        "Y": 8.1}, {"X": 9.8, "Y": 3.1}, {"X": 3.4, "Y": 3.9                }], "coordinates": [{"Type": "LineString", "coordinates": [{"X": 3.4,  "Y": 3.9}, {"X": 6.2, "Y": 8.1}, {"X":        9.8, "Y": 3.1}, {"X": 3.4, "Y": 3.9}]}], "Type": "Polygon"  } }  

The red logo above is the Geojsonpolygon JSON format.

It doesn't matter if you can't read points and coordinates here.

The following simulates a service A to another service B, the transport generic is a list of area, service b resolves the data, and returns the data.

The Controller for service A is to make a list of the generic type area, passing data to the B service through Resttemplate.

Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.data.geo.Point;ImportOrg.springframework.data.mongodb.core.geo.GeoJsonPolygon;Importorg.springframework.http.ResponseEntity;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.bind.annotation.RestController;Importorg.springframework.web.client.RestTemplate;Importjava.util.ArrayList;Importjava.util.List; @RestController Public classSendcontroller {@AutowiredPrivateresttemplate resttemplate; @GetMapping (Value= "/send") @ResponseBody PublicList Sendarea () {Point point1=NewPoint (3.4, 3.9); Point Point2=NewPoint (6.2, 8.1); Point Point3=NewPoint (9.8, 3.1); Point Point4=NewPoint (3.4, 3.9); List<Point> points =NewArraylist<>();        Points.Add (POINT1);        Points.Add (Point2);        Points.Add (POINT3);        Points.Add (POINT4); List<Area> arealist =NewArraylist<>(); Arealist.add (NewArea ("AAAA",NewGeojsonpolygon (points)); Arealist.add (NewArea ("BBBB",NewGeojsonpolygon (points)); Arealist.add (NewArea ("CCCC",NewGeojsonpolygon (points)); String URL= receivecontroller.base_url+receivecontroller.post_mapping; Responseentity Entity= resttemplate.postforentity (URL,arealist, List.class); List Body=(List) entity.getbody (); returnbody; } @Bean Publicresttemplate resttemplate () {return  Newresttemplate (); }}

Controller for service B

ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONObject;Importcom.alibaba.fastjson.TypeReference;ImportOrg.springframework.data.geo.Point;ImportOrg.springframework.data.mongodb.core.geo.GeoJsonPolygon;Importorg.springframework.web.bind.annotation.PostMapping;ImportOrg.springframework.web.bind.annotation.RequestBody;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.bind.annotation.RestController;Importjava.util.ArrayList;Importjava.util.List; @RestController Public classReceivecontroller { Public Static FinalString base_url = "http://localhost:8080";  Public Static FinalString post_mapping = "/receive"; @PostMapping (Value=post_mapping) @ResponseBody Publiclist arealist (@RequestBody String arealist) {list<Area> list =NewArraylist<>(); if(Arealist = =NULL ){            returnlist; }        /*** list<area> liststring = Json.parseobject (arealist, New typereference<list<area>> () {}); * Note that this is wrong, the area contains the attribute Geojsonpolygon, cannot parse * have to be a list of generics written as String*/List<String> liststring = Json.parseobject (Arealist,NewTypereference<list<string>>(){}); Jsonobject Jsonobject=NULL; Jsonobject Polygonjsonobject=NULL; Geojsonpolygon Geojsonpolygon=NULL;  for(inti=0; I < liststring.size (); i++) {String s=Liststring.get (i); Jsonobject=Jsonobject.parseobject (s); //gets the value of key corresponding to the Jsonobject objectString name = jsonobject.getstring ("name"); //parsing complex Geojsonpolygon PropertiesString geojsonpolygonstring = jsonobject.getstring ("Geojsonpolygon"); Polygonjsonobject=Jsonobject.parseobject (geojsonpolygonstring); String pointsstring= Polygonjsonobject.getstring ("Points"); List<Point> points = Json.parseobject (pointsstring,NewTypereference<list<point>>() {}); Geojsonpolygon=NewGeojsonpolygon (points); Area Area=NewArea ();            Area.setname (name);            Area.setgeojsonpolygon (Geojsonpolygon);        List.add (area); }        returnlist; }}

Note: using the Fastjson version is 1.2.47, if the version 1.2.7 is executed in this statement list<point> points = Json.parseobject ( Pointsstring, New Typereference<list<point>> () {}) will error, Because 1.2.7 requires that the generic class point here must have a parameterless constructor, the 1.2.47 version does not have a parameterless constructor.

A. Service b first uses the @requestbody annotation and then parses the nested data type;

B. If the List is using list<String> liststring = Json.parseobject (arealist, new typereference <List<String>> () {}) First resolves to a List of generic String . Because the Geojsonpolygon is too complex, can not be resolved directly, if it is simple as point list<point> points = Json.parseobject (pointsstring, new Typereference<list<point>> () {});

C. If it is not a list, and the string object is nested JSON data format, the string object is parsed into a jsonobject object;

D. Use the getString method of the Jsonobject object to obtain a string of the corresponding property key, if the string is still a complex JSON data, perform a B or C step until you get the data you want or the resolution is complete.

Online json:http://www.bejson.com/

   

Complex nested data type JSON format conversion (Geojsonpolygon) using Fastjson in network transport

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.