简体   繁体   中英

Parse Json objects with different types using gson

I have the following json from the Server. It is a json array with different objects. I want to identify the user objects based on the key "type" and add them to a user hashmap and fetch user to show information in my view containing the "payments" object. I am using gson and retrofit. TIA

"included":[
      {
         "id":"1",
         "type":"payments",
         "attributes":{
            "amount_cents":100,
            "amount_currency":"INR",
            "description":"Test description!!",
            "created_at":"2016-03-01T11:30:53Z",
            "status":"paid",
            "paid_at":null,
            "charged_at":null,
            "formatted_amount":"Rs1.00"
         },
         "relationships":{
            "sender":{
               "data":{
                  "id":"2",
                  "type":"users"
               }
            },
            "receiver":{
               "data":{
                  "id":"1",
                  "type":"users"
               }
            }
         }
      },
      {
         "id":"2",
         "type":"users",
         "attributes":{
            "first_name":"Rob",
            "last_name":"Thomas"
         }
      },
      {
         "id":"1",
         "type":"users",
         "attributes":{
            "first_name":"Matt",
            "last_name":"Thomas"
         }
      }]

My classes are

public class ActivityFeedItem implements IFeedItem {
    @SerializedName("id")
    String id;

    @SerializedName("type")
    String type;

    @SerializedName("attributes")
    Attributes attributes;

    protected class Attributes {
        double amount_cents;
        String amount_currency;
        String description;
        String created_at;
        String status;
        String paid_at;
        String charged_at;
        String formatted_amount;

        Relationships relationships;

        public double getAmount_cents() {
            return amount_cents;
        }

        public String getAmount_currency() {
            return amount_currency;
        }

        public String getDescription() {
            return description;
        }

        public String getCreated_at() {
            return created_at;
        }

        public String getStatus() {
            return status;
        }

        public String getPaid_at() {
            return paid_at;
        }

        public String getCharged_at() {
            return charged_at;
        }

        public String getFormatted_amount() {
            return formatted_amount;
        }

        public Relationships getRelationships() {
            return relationships;
        }
    }
}

and

public class UserFeedItem implements IFeedItem {

    @SerializedName("id")
    String id;

    @SerializedName("type")
    String type;

    @SerializedName("attributes")
    Attributes attributes;


    public class Attributes {
        @SerializedName("first_name")
        String first_name;

        @SerializedName("last_name")
        String last_name;
    }
}

This is pretty easy if you just put your JSON response String into a JSONArray . Then you can just access the type field and test if it's users . Like this:

JSONArray jsonArray = new JSONArray(yourServerResponseString);
for(int i=0; i<jsonArray.length(); i++) {
    JSONObject object = (JSONObject) jsonArray.get(i);
    String type = object.getString("type");
    if(type.equals("users")) {
        //add to your users HashMap
    }
}

First of all create an array of objects from you JSON using GSON as below

   Gson gson= new Gson();
   String jsonString= yourJsonObject.getString("included");
   ActivityFeedItem[] activityFeedArray=gson.fromJson(jsonString,ActivityFeedItem[].class);

Now your activityFeedArray contains all the feedItems you get in JSON. Then you can iterate through it as you would in any array and add to hashmap when type is user as below-

    for(ActivityFeedItem item:activityFeedArray) {
      if(item.type.equals("users")) {
        //add to your users HashMap
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM