java - 使用gson解析不同类型的Json对象

标签 java android json retrofit2 gson

我有来自服务器的以下 json。它是一个包含不同对象的 json 数组。我想根据键“类型”识别用户对象,并将它们添加到用户 HashMap 中,并获取用户以在包含“付款”对象的 View 中显示信息。我正在使用 gson 和 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"
         }
      }]

我的类(class)是

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;
        }
    }
}

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;
    }
}

最佳答案

如果您只需将 JSON 响应字符串放入 JSONArray 中,这非常简单。然后您只需访问 type 字段并测试它是否是 users。像这样:

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
    }
}

关于java - 使用gson解析不同类型的Json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760159/

相关文章:

java - 更新 OpenGL libGDX 中的边界框

android - 没有可用于为平台 'armeabi-v7a' 构建的工具链

android - OkHttp 请求错误链接时崩溃

javascript - 从字符串中提取 JSON

asp.net-mvc - 在 ASP.NET MVC 应用程序中为 json 输出过滤域对象的最佳方法

java - 如何通过 Web 套接字向连接的用户发送消息?

java - JPA条件查询有3个条件

java - 使用 Java 流将列表转换为映射

Android Studio 导入 facebook library odyssey

javascript - 计算总时间