Yarh
Yarh

Reputation: 4607

How to parse json of variable type with GSON?

I recive object like that

{
  "data": "some data",
  "social": {
    "twitter": "id"
  }
}

This is easly parsed using next classes

public class SocialLinks {

@Expose
private String data;
@Expose
private Social social;
}
public class Social {

@Expose
private String twitter;
}

Unfortunatly due to some issues, if social is empty it is returened as array

{
  "data": "some data",
  "social": [

  ]
}

How can I parse it with gson? (I am not a developer of server side and cannot affect responce meassages)

Upvotes: 1

Views: 1939

Answers (1)

bhdrkn
bhdrkn

Reputation: 6702

You can do that using these classes.

SocialLinks.java

public class SocialLinks {
    private String data;
    private Social social;
    // Getters && Setters
}

Social.java:

public class Social {

    private String twitter;
    // Getters & Setters
}

And here is your main method

public class GsonApp {

    private static final String TEST_JSON = "{\n" +
            "  \"data\": \"some data\",\n" +
            "  \"social\": {\n" +
            "    \"twitter\": \"id\"\n" +
            "  }\n" +
            "}";


    public static void main(String[] args) throws Exception {
        final Gson gson = new GsonBuilder().create();
        // Read Example
        final SocialLinks socialLinks = gson.fromJson(TEST_JSON, SocialLinks.class);
        System.out.println(gson.toJson(socialLinks));

        // Write with null Social 
        final SocialLinks socialLinks1 = new SocialLinks();
        socialLinks1.setData("MyData");
        System.out.println(gson.toJson(socialLinks1));

        // Write with empty Social (social.twitter is null)    
        final SocialLinks socialLinks2 = new SocialLinks();
        socialLinks2.setData("MyData");
        socialLinks2.setSocial(new Social());
        System.out.println(gson.toJson(socialLinks2));

        // Write with full Social
        final SocialLinks socialLinks3 = new SocialLinks();
        socialLinks3.setData("MyData");
        socialLinks3.setSocial(new Social());
        socialLinks3.getSocial().setTwitter("ID");
        System.out.println(gson.toJson(socialLinks3));
    }
}

This will output

{"data":"some data","social":{"twitter":"id"}}
{"data":"MyData"}
{"data":"MyData","social":{}}
{"data":"MyData","social":{"twitter":"ID"}}

Update

If you data type changes depending on your application state you may want to create Map object instead of DTO. Here is an example

private static final String TEST_JSON_2 = "{\n" +
        "  \"data\": \"some data\",\n" +
        "  \"social\": [\n" +
        "  ]\n" +
        "}";

...

    Type type = new TypeToken<Map<String, Object>>(){}.getType();
    final Map<String, Object> socialLinks4 = gson.fromJson(TEST_JSON_2, type);
    System.out.println(socialLinks4);

    final Map<String, Object> socialLinks5 = gson.fromJson(TEST_JSON, type);
    System.out.println(socialLinks5);

This will output

{data=some data, social=[]}
{data=some data, social={twitter=id}}

Upvotes: 1

Related Questions