2

Is it possible to deserialize complex JSON using Gson like this

{
    item1:"text1"
    item2:{
        item3:"text3"
        item4:"text4"
    }
}

To:

class MyClass {
    String item1;  //item1 == "text1";
    String item4;  //item4 == "text4";
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Marik
  • 47
  • 2
  • Why would item2 and item3 disapear from the pattern and why would item4 be a direct child of the main object? Can you describe what you want exactly? – Lajos Arpad Dec 21 '15 at 15:40

4 Answers4

2

The correct models would be:

class MyClass {
    String item1;  
    Item2 item2;  
}

class Item2{
    String item3; 
    String item4;
}
Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53
  • Yes it is the full model representing the input but what if `MyClass` cannot be modified ? I believe this is what OP is asking for. – Gaël J Dec 21 '15 at 16:15
  • 1
    @Gaël Yeah, after I saw your answer, I thought that that might be the case, that's why I upvoted you. Though I'm gonna leave this answer here in case he/she was actually asking for the models :) – Rick Sanchez Dec 21 '15 at 16:24
1

If you can't change your model, i.e. MyClass, you will have to write a custom deserializer. See How do I write a custom JSON deserializer for Gson? for example.

In your case, it could be something like this :

@Override
public MyClass deserialize(JsonElement json, Type type,
        JsonDeserializationContext context) throws JsonParseException {

    JsonObject jobject = json.getAsJsonObject();

    String item1 = jobject.get("item1").getAsString();
    String item4 = jobject.get("item2").getAsJsonObject().get("item4").getAsString(); // TODO check for NPE

    return new MyClass(item1, item4);
}

EDIT : you don't have to deserialize everything manually, if instead of Strings you have complex objects you can still partially invoke Gson on these objects (as mentionned in the link above).

Community
  • 1
  • 1
Gaël J
  • 11,274
  • 4
  • 17
  • 32
0

Parsing out JSON like this shouldn't be too difficult. This is how I would do it.

public void parseJSON(JSONObject input) {
    MyClass myClass = new MyClass(jsonObject);
}

public class MyClass {

    public JSONObject jsonObject;
    public String item1;
    public String item3;
    public String item4;

    public MyClass(JSONObject jsonObject) {
        this.jsonObject = jsonObject;
        parseInput();
    }

    public void parseInput() {
        try {
            item1 = (String) jsonObject.get("item1");
            JSONObject obj2 = (JSONObject) jsonObject.get("item2");
            item3 = (String) obj2.get("item3");
            item4 = (String) obj2.get("item4");

            Log.d("OUTPUT", item1+", "+item3+", "+item4);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
-1

This article might be helpful..

It has a working example of parsing a complex JSON like this..

{ "pageInfo": { "pageName": "TheTestbook", "pagePic": "content.jpg" }, "posts": [{ "postId": "123456", "postName": "Java Post" }] }

     public class JavaJsonSerializer {

     public static void main(String[] args) {
        String jsonString = "{  \"pageInfo\": {     \"pageName\": \"TheTestbook\",      \"pagePic\": \"content.jpg\"    },"
    + " \"posts\": [{       \"postId\": \"123456\",     \"postName\": \"Java Post\" }]}";

     Gson gson = new Gson();
     BlogPost blogPost = gson.fromJson(jsonString, BlogPost.class);
     System.out.println(blogPost);
 }

}

You can see the complete example here.. http://howtolearnjava.com/how-to-parse-json-in-java-marshaling-to-java-objects/

Ravinder G
  • 36
  • 5