Naga
Naga

Reputation: 2021

how can I parse json using gson with Json Array of json objects

below is my json which I want to parse using json.

{  
   "status":{  
      "code":200
   },
   "authKey":"xyz",
   "requireUpdate":false,
   "features":[  
      {  
         "accounting":true
      },
      {  
         "channel":true
      },
      {  
         "skipfornow":false
      },

      {  
         "xyz":false
      },
     {  
         "abc":false 
      }
   ]
}

Model class

public class LoginResponse
{
    private String authKey;

    private Status status;

    private Features[] features;

    private String requireUpdate;

    public String getAuthKey ()
    {
        return authKey;
    }

    public void setAuthKey (String authKey)
    {
        this.authKey = authKey;
    }

    public Status getStatus ()
    {
        return status;
    }

    public void setStatus (Status status)
    {
        this.status = status;
    }

    public Features[] getFeatures ()
    {
        return features;
    }

    public void setFeatures (Features[] features)
    {
        this.features = features;
    }

    public String getRequireUpdate ()
    {
        return requireUpdate;
    }

    public void setRequireUpdate (String requireUpdate)
    {
        this.requireUpdate = requireUpdate;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [authKey = "+authKey+", status = "+status+", features = "+features+", requireUpdate = "+requireUpdate+"]";
    }
}



public class Status
{
    private String code;

    public String getCode ()
    {
        return code;
    }

    public void setCode (String code)
    {
        this.code = code;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [code = "+code+"]";
    }
}

public class Features
{
    private String accounting;

    public String getAccounting ()
    {
        return accounting;
    }

    public void setAccounting (String accounting)
    {
        this.accounting = accounting;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [accounting = "+accounting+"]";
    }
}

As you can see features field in json is a json array, everything is working fine except values in features. From the Feature class I am only getting value for accounting but I want value for channel and skipfornow keys as well , how can I get this. features json Array can have lets say 100 json objects ,

for example features json array has 100 json objects and each json object has different key value pair , it comes from server, some time it may have 50 json objects and some time it may have 100 json objects and each objects have different key

then how should I create the feature model ?.

Upvotes: 0

Views: 178

Answers (2)

ThomasThiebaud
ThomasThiebaud

Reputation: 11969

You need to add channel and skipfornow in the Features class

public class Features {
    private String accounting;
    private String channel;
    private String skipfornow;

    //Getters && Setters
    //...
}

Can you explain more this sentence ?

features json Array can have lets say 100 json objects then how should I create the feature model ?.

You mean that you can have other object like id or whatever ? If so you have to add all the object possibilies in the Features class. If the json object is present you will have the value, otherwise you will get the default value.

EDIT Maybe you can do someting if you create your own deserializer (this this thread). Here is an idea (just an idea, I can't test it right now). I hope it will help you.

/**
 * New Pojo
 */
public class Features {
    private Set<Map.Entry<String,JsonElement>> entries;

    public void add(Map.Entry<String,JsonElement> element) {
        entries.add(element);
    }
}

/**
 * Features deserializer
 */
public class FeaturesDeserializer implements JsonDeserializer<Features> {
    @Override
    public FeaturesDeserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {

        JsonArray jarray = json.getAsJsonArray();

        Features features = new Features();

        for(int i=0; i< jarray.size(); i++) {
            JsonObject jobject = jarray.get(i).getAsJsonObject();
            //Use of entrySet because your object's name are random
            for(Map.Entry<String,JsonElement> m : jobject.entrySet()) {
                features.add(m);
            }
        }

        return features;
    }
}

Upvotes: 1

Don Chakkappan
Don Chakkappan

Reputation: 7560

Create a class Features with members accounting,channel,skipfornow.

And in your LoginResponse class use Features with a object features .

Gson will automatically detect all of these.Object reference name is highly important.

Upvotes: 0

Related Questions