2015-05-15 240 views
1

我有一個JSON:GSON反序列化

"account_representatives": [ 
     { 
     "Sales Person": 1307, 
     "default_ticket_assignments": [ 
      "" 
     ], 
     "Primary Account Manager": 1307, 
     "Secondary Support-3": 1151, 
     "Relationship Mgr": 1307, 
     "authorized_resources": [ 
      "" 
     ], 
     "Technical Account Manager": 164 
     } 
    ] 

,我有一個類,它的結構是這樣的:

public class AccountRepresentative { 

     @SerializedName("authorized_resources") 
     @Expose 
     private List<String> authorizedResources = new ArrayList<String>(); 
     @SerializedName("default_ticket_assignments") 
     @Expose 
     private List<String> defaultTicketAssignments = new ArrayList<String>(); 
     @Expose 
     private Map<String, String> repFields; 

     /** 
     * @return The authorizedResources 
     */ 
     public List<String> getAuthorizedResources() { 
      return authorizedResources; 
     } 

     /** 
     * @param authorizedResources The authorized_resources 
     */ 
     public void setAuthorizedResources(List<String> authorizedResources) { 
      this.authorizedResources = authorizedResources; 
     } 

     /** 
     * @return The defaultTicketAssignments 
     */ 
     public List<String> getDefaultTicketAssignments() { 
      return defaultTicketAssignments; 
     } 

     /** 
     * @param defaultTicketAssignments The default_ticket_assignments 
     */ 
     public void setDefaultTicketAssignments(List<String> defaultTicketAssignments) { 
      this.defaultTicketAssignments = defaultTicketAssignments; 
     } 

     /** 
     * 
     * @return the dynamic jsonObjects 
     */ 
     public Map<String, String> getRepFields() { 
      return repFields; 
     } 

     /** 
     * @param repFields The repFields 
     */ 
     public void setRepFields(Map<String, String> repFields) { 
      this.repFields = repFields; 
     } 
    } 

我使用GSON結合這些,我能夠綁定2 JsonArrays。

我也可以W/O的jsonArrays使用How can I convert JSON to a HashMap using Gson?

但在這種情況下,反序列化的動態性能,我不知道怎麼弄的動態特性, 我的計劃據說是把動態特性爲hashmap,但2 jsonArrays的方式。

感謝您的幫助,

+0

你的意思是比如說,你不確定'鍵值對'的關鍵?它可以改變,恕不另行通知? –

+0

是的..鑰匙是動態的,所以我不能在飛行中抓住他們 – Keiichi

回答

0

我已經找到了如何使用GSON定製解串器感謝這個帖子,Using GSON to parse array with multiple types,也是這個帖子,java collections - keyset() vs entrySet() in map 我所做的是創建這個定製解串器:

package com.xxx.xyz.models; 

import android.util.Log; 

import com.google.gson.JsonArray; 
import com.google.gson.JsonDeserializationContext; 
import com.google.gson.JsonDeserializer; 
import com.google.gson.JsonElement; 
import com.google.gson.JsonObject; 
import com.google.gson.JsonParseException; 

import java.lang.reflect.Type; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 
import java.util.Map.Entry; 
import java.util.Set; 

public class AccountRepresentativeDeserializer implements JsonDeserializer<AccountDetailsModel.AccountRepresentative> { 
    @Override 
    public AccountDetailsModel.AccountRepresentative deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 
     Log.v("AUTHORIZED REP ", json.toString()); 
     AccountDetailsModel.AccountRepresentative accountRep = new AccountDetailsModel().new AccountRepresentative(); 
     JsonObject jObject = json.getAsJsonObject(); 
     Set<Entry<String, JsonElement>> items = jObject.entrySet(); 
     Iterator<Entry<String, JsonElement>> it = items.iterator(); 
     Map<String, String> repFields = new HashMap<>(); 

     while (it.hasNext()) { 
      Entry<String, JsonElement> i = it.next(); 
      if (i.getKey().equals("default_ticket_assignments")) { 
       ArrayList<String> defaultTicketAssignments = new ArrayList<>(); 
       JsonArray defTicketAssignment = i.getValue().getAsJsonArray(); 
       Iterator<JsonElement> defTicketIterator = defTicketAssignment.iterator(); 

       while (defTicketIterator.hasNext()) { 
        JsonElement defTicketItem = defTicketIterator.next(); 
        defaultTicketAssignments.add(defTicketItem.getAsString()); 
       } 
       accountRep.setDefaultTicketAssignments(defaultTicketAssignments); 
      } else if (i.getKey().equals("authorized_resources")) { 
       ArrayList<String> authorizedResources = new ArrayList<>(); 
       JsonArray authorizedRes = i.getValue().getAsJsonArray(); 
       Iterator<JsonElement> authorizedResIterator = authorizedRes.iterator(); 

       while (authorizedResIterator.hasNext()) { 
        JsonElement defTicketItem = authorizedResIterator.next(); 
        authorizedResources.add(defTicketItem.getAsString()); 
       } 
       accountRep.setAuthorizedResources(authorizedResources); 
      } else { 
       repFields.put(i.getKey(), i.getValue().getAsString()); 
      } 
     } 
     accountRep.setRepFields(repFields); 
     return accountRep; 
    } 
} 

然後使用它像這樣:

Gson gson = new GsonBuilder().registerTypeAdapter(AccountDetailsModel.AccountRepresentative.class, new AccountRepresentativeDeserializer()).create(); 
         AccountDetailsModel result = gson.fromJson(strResult, AccountDetailsModel.class); 

順便說一句, 的AccountRep resentative是一個內部類,AccountRepresentative中的兩個數組總是在JSON中,只有屬性是動態的。

我想我將無法解析內部一個,併爲AccountDetailModel本身的自定義解串器,但我想以創建AccountRepresentative解串器,它都很好:)