0

I have a json of the below format

[
    {
        "id": "one",
        "type": "Integer",
        "value": "10"
    },
    {
        "id": "two",
        "type": "String",
        "value": "StringValue"
    },
    {
        "id": "three",
        "type": "com.something.special",
        "value": {
            "splFiel1": "filedOne",
            "splFiel2": "fielTwo",
            "splFiel3": "fieldThree"
        }
    }
]

Each array element always will have three fields id,type and value. The structure of the field "value" will depend on the field "type" and can change based on that.

I would like to convert this json into Java object, so that i can easily access "value" obj and its sub fields easily . The reason i don't consider this as normal json to java object conversion is due to the varying field structure of "value" field based on the field "type" in the same json.

Can this be done?. I am trying to do this with jackson json, but please do suggest if you have better options.

Please provide any ideas, suggestions, reference links.

1
  • I suggest you use org.json to parse the string as an array first, loop through each object, use the get method of the parsed object to find the type and then map the object to that class. Commented Dec 26, 2016 at 12:42

2 Answers 2

1

You can use following POJO for conversion of your given JSON

public class Example {

@SerializedName("id")
private String id;
@SerializedName("type")
private String type;
@SerializedName("value")
private String value;
}

For the third field, you can keep it simple string. And then whenever you wish to parse its contents to proper construct java class, you can check the type in it and parse the json string into some java object

Sign up to request clarification or add additional context in comments.

Comments

1

The JSON file is read using the Google GSON library.

Make the DataStructure store the JSON data. The value field of dataStructure is string type. If it stores the JSON string, then again do the JSON parse.

class Data{

    String id;
    String type;
    String value;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    @Override
    public String toString() {
    return "Data [id=" + id + ", type=" + type + ", value=" + value + "]";
    }

}

public class JSONData {

    public static void main(String[] args) throws FileNotFoundException{

         Gson gson = new Gson();
         JsonParser parser = new JsonParser();

        JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream("in.json")));

        JsonArray jArray = parser.parse(reader).getAsJsonArray();

        List<Data> datas = new ArrayList<>();

        for (JsonElement object : jArray) {

            Data data = new Data();

            JsonObject jObject = gson.fromJson(object, JsonObject.class);

            data.setId(jObject.get("id").toString());
            data.setType(jObject.get("type").toString());
            data.setValue(jObject.get("value").toString());

            System.out.println(data);
            datas.add(data);
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.