【问题标题】:Parse Json objects with different types using gson使用 gson 解析不同类型的 Json 对象
【发布时间】:2016-03-02 23:17:13
【问题描述】:

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

我的课是

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

【问题讨论】:

  • 循环遍历included 数组并将其添加到您的用户哈希图中。就像@NoChinDeluxe 做的那样,但现在使用 gson 类数组。
  • 在 JSON 对象中,第一个对象的属性与第二个和第三个对象相比是不同的,在这种情况下您可以如何检索。在迭代中它会找到不同的键。

标签: java android json retrofit2 gson


【解决方案1】:

如果您只需将 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
    }
}

【讨论】:

  • 我知道这种方法。但是如果我使用 Gson 作为 gson 处理对象的序列化,我该怎么做呢?
  • 由于您的服务器响应是 JSON,您基本上可以使用上述方法解析它并提取创建 ActivityFeedItemUserFeedItem 对象所需的值。然后你会用 Gson 序列化它们。除非我在您尝试做的事情中遗漏了什么,否则请随时指出这一点。
【解决方案2】:

首先使用 GSON 从您的 JSON 中创建一个对象数组,如下所示

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

现在您的 activityFeedArray 包含您在 JSON 中获得的所有 feedItem。然后,您可以像在任何数组中一样遍历它,并在类型为用户时添加到 hashmap,如下所示-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多