Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:35:53+00:00 2026-06-08T02:35:53+00:00

I am using google gson-2.2.1 library for parsing large response of JSON. I have

  • 0

I am using google gson-2.2.1 library for parsing large response of JSON.

I have to parse a JSON response where structure may vary.

First case, when the response contains more than one team:

 "Details":{

           "Role":"abc",
           "Team":[
              {
                 "active":"yes",
                 "primary":"yes",
                 "content":"abc"
              },
              {
                 "active":"yes",
                 "primary":"yes",
                 "content":"xyz"
              }
           ],

Second case, when only one team is passed:

"Details":{

           "Role":"abc",
           "Team":
              {
                 "active":"yes",
                 "primary":"yes",
                 "content":"abc"
              }
}

There are my base classes used for parsing:

class Details {
    public String Role;
    public ArrayList<PlayerTeams> Team = new ArrayList<PlayerTeams>();
        PlayerTeams Team; // when JsonObject
}

class PlayerTeams {
    public String active;
    public String primary;
    public String content;
}

The problem is that I can not use ArrayList<PlayerTeams> when I have only one of them and it’s returned as JsonObject.

Gson can identify static format of JSON response. I can trace full response dynamically by checking if “Team” key is instance of JsonArray or JsonObject but it would be great if a better solution is available for that.

Edit :
If my response is more dynamic..

"Details":{

       "Role":"abc",
       "Team":
          {
             "active":"yes",
             "primary":"yes",
             "content":"abc"
             "Test":
             {
                 "key1":"value1",
                 "key2":"value2",
                 "key3":"value3"
             }
          }
}

In my edited question, I am facing problem while my response is more dynamic..Team and Test can be JsonArray or JsonObject.. It really harassing me because sometime Test object may array when more data, may object when single data, string when no data. There is no consistency in response.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:35:55+00:00Added an answer on June 8, 2026 at 2:35 am

    You need a type adapter. This adapter would be able to distinguish which format is coming and instance the right object with the right values.

    You can do this by:

    1. implement your own type adapter by creating a class that implements JsonSerializer<List<Team>>, JsonDeserializer<List<Team>>, of course JsonSerializer is just needed in case you need to serialize it in that matter too.
    2. Register the type adapter to you GsonBuilder like: new GsonBuilder().registerTypeAdapter(new TypeToken<List<Team>>() {}.getType(), new CoupleAdapter()).create()

    The deserialize method could look like:

    public List<Team> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
            throws com.google.gson.JsonParseException {
        if (json.isJsonObject()) {
            return Collections.singleton(context.deserialize(json, Team.class));
        } else {
            return context.deserialize(json, typeOfT);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I am using Google's Gson library to parse JSON into Java data structures.
I am parsing a POJO into json using Google's gson library. These are the
I'm using Google's GSON Library to convert JSON data to Java objects. The issue
I want to parse json data into java object using google-gson . Here is
I'm trying to parse json request using google GSON (on Android although this is
I'm parsing JSON using GSON, but get an error when I try to parse
I am using Gson library to convert objects to Json and vice versa. I
I am trying to convert an object into JSON using the GSON library on
I am trying to parse a json object using gson in Android application ,the
I'm using the GSON 1.7.1 library to create a JSON representation of a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.