2016-03-02 40 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 
     } 
    }