Get JSON data with GSON in Java

1

I need to get the data progress and tres (see example below) but I do not know how.

Also in some occasions I will have to recover more data of the type class without knowing in advance how many will be.

I'm looking at examples of using GSON but I can not. Any ideas or documentation that I can look at? Thank you very much.

The JSON code is as follows:

{
    "images": [
        {
            "classifiers": [
                {
                    "classifier_id": "clasif",
                    "name": "nombre",
                    "classes": [
                        {
                            "class": "progress",
                            "score": 0.770309
                        },
                        {
                            "class": "tres",
                            "score": 0.599846
                        }
                    ]
                }
            ],
            "image": "imagen.jpg"
        }
    ],
    "images_processed": 1,
    "custom_classes": 6
}
    
asked by Silvia 20.02.2018 в 18:33
source

1 answer

1

Consider the following example taken from the documentation of Gson :

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}

// Serialization
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  

// ==> json is {"value1":1,"value2":"abc"}

// Deserialization
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
// ==> obj2 is just like obj

In your case you must create a java class that contains the attributes defined in the json and then invoke new Gson().fromJson(jsonString, TuClase.class)

    
answered by 20.02.2018 в 19:42