0

Here is the Json response I am trying to parse using Gson. But i am having trouble creating java class which i am going to pass to fromJson method. Could anybody help me with this?

 {
        "id":5,
        "name":"1322",
        "number":"1",
        "polygons":[
            [
                [
                    [-122.00895192246342,37.41167155605386],
                    [-122.0089070066992,37.41181283360041],
                    [-122.0087533947856,37.411780725090495],
                    [-122.00879741223456,37.411636593387556],
                    [-122.00895192246342,37.41167155605386]
                ],
                0,
                "#11d4a0",
                "A Zone Name"
            ]
        ]
    }
Angel
  • 191
  • 4
  • 4
  • 12
  • 1
    Have you see this examples http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ or http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java – Reddymails Oct 30 '14 at 20:32

2 Answers2

0

This data structure might make more sense:

{
        "id":5,
        "name":"1322",
        "number":"1",
        "polygons":[
            {
                [
                    [-122.00895192246342,37.41167155605386],
                    [-122.0089070066992,37.41181283360041],
                    [-122.0087533947856,37.411780725090495],
                    [-122.00879741223456,37.411636593387556],
                    [-122.00895192246342,37.41167155605386]
                ],
                0,
                "#11d4a0",
                "A Zone Name"
            }
        ]
    }
pherris
  • 17,195
  • 8
  • 42
  • 58
  • The things is i can't alter this json response... its what my code gets and i have to write parser to store it as java object – Angel Oct 30 '14 at 21:21
  • 1
    My question is what datatype i should declare for the above? – Angel Oct 30 '14 at 21:22
  • Agree that this would be better as a comment, but the formatting would be completely lost and basically unusable. – pherris Nov 03 '14 at 19:31
0

Use below class:

public class Example {
    private int id;
    private String name;
    private String number;

    private List<List<Object>> polygons;
}

Test:

String json = "{\"id\":5,\"name\":\"1322\",\"number\":\"1\"," + "\"polygons\":[[[[-122.00895192246342,37.41167155605386]," + "[-122.0089070066992,37.41181283360041],"
        + "[-122.0087533947856,37.411780725090495]," + "[-122.00879741223456,37.411636593387556]," + "[-122.00895192246342,37.41167155605386]]," + "0," + "\"#11d4a0\"," + "\"A Zone Name\"]]}";

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();

Example example = gson.fromJson(json, Example.class);

Validate solution:

Inner list will be deserialized as an object which may be another List or a String or a Double value for your sample json. Loop over that list and get values. For instance:

for (int i = 0; i < example.polygons.get(0).size(); i++) {

    Object polygon = example.polygons.get(0).get(i);
    if (polygon instanceof List) {
        for (int j = 0; j < ((List) polygon).size(); j++) {
            Object innerArray0 = ((List) polygon).get(j);
            System.out.print("Item#" + i + "." + j);
            if (innerArray0 instanceof List) {
                for (int k = 0; k < ((List) innerArray0).size(); k++) {
                    Object value = ((List) innerArray0).get(k);
                    System.out.print(" : value[" + k + "] :" + value + " ");
                }
            }
            System.out.println("");
        }
    } else if (polygon instanceof Double || polygon instanceof String) {
        System.out.println("Item#" + i + " : value : " + polygon);
    }
    System.out.println("");
}

System.Out:

Item#0.0 : value[0] :-122.00895192246342  : value[1] :37.41167155605386 
Item#0.1 : value[0] :-122.0089070066992  : value[1] :37.41181283360041 
Item#0.2 : value[0] :-122.0087533947856  : value[1] :37.411780725090495 
Item#0.3 : value[0] :-122.00879741223456  : value[1] :37.411636593387556 
Item#0.4 : value[0] :-122.00895192246342  : value[1] :37.41167155605386 
Item#1 : value : 0.0
Item#2 : value : #11d4a0
Item#3 : value : A Zone Name
Devrim
  • 15,345
  • 4
  • 66
  • 74