2016-03-02 44 views
1

从服务器中获取以下json。它是一个具有不同对象的json数组。我想根据“type”键识别用户对象,并将它们添加到用户hashmap中,并获取用户以在我的视图中显示包含“payments”对象的信息。我正在使用gson和翻新。 TIA使用gson解析具有不同类型的Json对象

"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; 
    } 
} 
+0

通过'included'数组循环,并将其添加到您的用户hashmap。就像@NoChinDeluxe正在做的一样,但现在使用gson类数组。 – Bharatesh

+0

在JSON对象中,第一个Object属性与第二个和第三个对象不同,在这种情况下,您可以如何检索。在迭代中,它会找到不同的键。 – Harish

回答

0

这是很容易的,如果你只是把你的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 
    } 
} 
+0

我知道这种方法。但是,如果我使用Gson作为gson处理对象的序列化,我在哪里可以做到这一点? –

+0

由于您的服务器响应是JSON,因此您可以使用上述方法将其解析出来,并提取创建“ActivityFeedItem”和“UserFeedItem”对象所需的值。然后你会使用Gson对它们进行序列化。除非我在你想要做的事情中缺少某些东西,在这种情况下可以随意指出。 – NoChinDeluxe

0

首先创建一个使用JSON GSON从你对象的数组如下

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

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

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