Parse JSON Complex

2

I'm trying to parse a JSON, using GSON, but there is a complex part, because I do not know the name of the keys, so I do not know what names to use in the variables for GSON to parse. I searched and it looks like I have to use Map < & gt ;, but even so I can not.

JSON example:

{
  "type": "typeString",
  "format": "json",
  "version": "4.13.35",
  "data": {
    "data1": {
      "version": "4.13.1",
      "id": "data1",
      "key": "238",
      "name": "John"
    },
    "data2": {
      "version": "4.13.1",
      "id": "data2",
      "key": "115",
      "name": "Hello"
    },
    "data3": {
      "version": "4.13.1",
      "id": "date3",
      "key": "26",
      "name": "Zeus"
    },
    "data4": {
      "version": "4.13.1",
      "id": "data4",
      "key": "143",
      "name": "Venus"
    }
  }
}

Class made:

public class MasterGSON {

    @SuppressWarnings("type")
    private String type;

    @SuppressWarnings("format")
    private String format;

    @SuppressWarnings("version")
    private String version;

    @SuppressWarnings("data")
    private Map<String, DataGSON> listChampion;

    public Map<String, DataGSON> getListChampion() {
        return listChampion;
    }

    public String getType() {
        return type;
    }

    public String getFormat() {
        return format;
    }

    public String getVersion() {
        return version;
    }

    public class DataGSON {


        private String version;

        private String id;

        private String key;

        private String name;


        public String getVersion() {
            return version;
        }

        public String getId() {
            return id;
        }

        public String getKey() {
            return key;
        }

        public String getName() {
            return name;
        }
    }
}

Can someone help me?

    
asked by anonymous 27.08.2014 / 16:58

3 answers

3

GSON parses correctly if the field name is the same as JSON. Because it uses Reflection to deserialize the nested object.

The listChampion attribute is null because it makes a direct association of the JSON keys with its object. It finds the data key and by reflection searches for an attribute named data in its MasterGSON class. Then it checks the type, and deserializes the type. In the case of Map , no matter the name of the keys, what will matter is the internal structure of the object DataGSON .

Only by modifying the attribute name from listChampion to data , but keeping Map<String, DataGSON> it deserializes correctly.

The class would look like:

public class MasterGSON {

    @SuppressWarnings("type")
    private String type;

    @SuppressWarnings("format")
    private String format;

    @SuppressWarnings("version")
    private String version;

    @SuppressWarnings("data")
    private Map<String, DataGSON> data;

    public Map<String, DataGSON> getListChampion() {
        return data;
    }

    // Demais getters/setters
}

To deserialize JSON I did it this way:

GsonBuilder gsonBuilder = new GsonBuilder();
MasterGSON gs = gsonBuilder.create().fromJson(json, MasterGSON.class);

In its Map<String, DataGSON> , where each element is a key / value pair of its data object. As long as the name of this object does not change, it does not matter the name of the keys inside it.

    
27.08.2014 / 18:17
0

You can use JSON Schema . You simply pass the JSON and it returns the POJO, then you can use the GSON.

I made a test with your JSON file and returned those classes, after it's generated just make some adjustments.     ----------------------------------- com.example.Data.java -------- ---------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Data {

@Expose
private Data1 data1;
@Expose
private Data2 data2;
@Expose
private Data3 data3;
@Expose
private Data4 data4;

public Data1 getData1() {
return data1;
}

public void setData1(Data1 data1) {
this.data1 = data1;
}

public Data2 getData2() {
return data2;
}

public void setData2(Data2 data2) {
this.data2 = data2;
}

public Data3 getData3() {
return data3;
}

public void setData3(Data3 data3) {
this.data3 = data3;
}

public Data4 getData4() {
return data4;
}

public void setData4(Data4 data4) {
this.data4 = data4;
}

}
-----------------------------------com.example.Data1.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Data1 {

@Expose
private String version;
@Expose
private String id;
@Expose
private String key;
@Expose
private String name;

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
-----------------------------------com.example.Data2.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Data2 {

@Expose
private String version;
@Expose
private String id;
@Expose
private String key;
@Expose
private String name;

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
-----------------------------------com.example.Data3.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Data3 {

@Expose
private String version;
@Expose
private String id;
@Expose
private String key;
@Expose
private String name;

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
-----------------------------------com.example.Data4.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Data4 {

@Expose
private String version;
@Expose
private String id;
@Expose
private String key;
@Expose
private String name;

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Example {

@Expose
private String type;
@Expose
private String format;
@Expose
private String version;
@Expose
private Data data;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

}
    
27.08.2014 / 17:10
0

Because the JSON schema is not static, you can use the JSON "DOM" that GSON provides (

27.08.2014 / 18:32