1

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)

2
  • can you paste your parsing code with gson? Commented Aug 6, 2015 at 10:11
  • @Nitesh I am using basic way: SocialLinks value = GSON.fromJSON(jsonString, SocialLinks.class); Commented Aug 6, 2015 at 10:36

1 Answer 1

1

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}}
Sign up to request clarification or add additional context in comments.

3 Comments

and that works well, i know it. My problem is - sometimes sosial changes its type from JSON object to JsonArray dues tos erver stange configuration.
@Yarh Which sides generates Json array? You (server)? Since I demonstrate almost all the alternatives, I do not think that generates a Json array for you. Do you mean, some times client side sends arrays instead of object?
Server side generates json responce fro my client side get request. If there are not items for social - social is an array, otherwise - social is an object. I

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.