javaarraystwitter-streaming-api

How do I convert a json field of nested arrays into a Java entity?


I have a json from the Twitter Stream API. A field in this json consists of a nested array.

This json field looks like this:

{
"bounding_box": {
"coordinates": [
  [
    [
      -74.026675,
      40.683935
    ],
    [
      -74.026675,
      40.877483
    ],
    [
      -73.910408,
      40.877483
    ],
    [
      -73.910408,
      40.3935
    ]
  ]
],
"type": "Polygon"
}
}

I create a java class called BoundingBox, and in it I define a variable of the type coordinates. What type should this variable have?

I need help with how to turn this space into a Java object. Can you please help me?


Solution

  • Actually your coordinates property is three dimensional array so to parse it you would have to use for example List<List<List<Double>>>. Your BoundingBox class could look like this :

    public class BoundingBox {
        private List<List<List<Double>>> coordinates;
        private String type;
        // constructors, getters, setter
    }
    

    Personally I think that storing data in three nested lists is a bad approach and you should think how to logically arrange those data. Maybe the most nested list represent x, y coordinates of some location and you could create another POJO that would actually make your data model more readable.