Jackson2 Java to Json array ignore field names when creating array -


I am trying to create a Java Rest Endpoint which gives a JSON data array to be consumed by the Jacquet float charting plugin is. / P>

At least, FLOT requires an array of JSON data numbers, i.e.

  [[x1, y1], [x2, y2], .. .]  

Looking at me that Java has a list of point objects

  list & lt; Point & gt; Data = new arreelist & lt; & Gt; ();  

where the point is defined

  public square point {private final integer x; Private final integer wi; Public point (integer x, integer y) {this.x = x; this. Y = y; } ...}  

What methods or Jackson 2 annotations, if any, I need to get the right object to get the correct JSON format. Currently I am getting the output in this format:

  [{x: x1, y: y1}, {x: x2, y: y2} ...]  < / Ex> 

When I need this format:

  [[x1, y1], [x2, y2] ...]  

You create a custom point serializer

  import java.io. IOException can write; Import org.codehaus.jackson.JsonGenerator; Import org.codehaus.jackson.JsonProcessingException; Import org.codehaus.jackson.map.JsonSerializer; Import org.codehaus.jackson.map.SerializerProvider; Public Category Extends Custom Point Examiner JsonSerializer & lt; Point & gt; {@ Override Public Watch Serialize (Point Point, JSogenzner General, serializer provider provider) throws IOException, Jason Processing Uption {gen.writeStartArray (); Gen.writeNumber (point.getX ()); Gen.writeNumber (point.getY ()); Gen.writeEndArray (); }}  

You can then set your custom serializer class to import your point class

  Codehaus.jackson.map .annotate.JsonSerialize; @JasonSerialize (= using custom point surrogacy class) public class point {personal integer x; Private integer wi Public point (integer x, integer y) {this.x = x; this. Y = y; } Public Integer getX () {Return x; } Public Zero setX (integer x) {this.x = x; } Public Integer getY () {return y; } Public Zero Set Y (Integer Y) {this.y = y; }}  

Try more

  ObjectMapper mapper = new ObjectMapper (); & Lt; Point & gt; Issue = New Arrestist & lt; Point & gt; (); Points.add (new point (1,2)); Points.add (new point (2,3)); Println (mapper.writeValueAsString (Points));  

The code generates the following results

  [[1,2], [2,3]]  

Hope it helps.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -