混合对象类型的反序列化



我有一个JSON:

"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通过使用我如何使用Gson JSON转换为HashMap ?

但是在这种情况下,我不知道如何获得动态属性,我的计划应该是将动态属性转换为hashmap,但2 jsonArrays是在方式。

谢谢你的帮助,

我已经发现了如何使用GSON自定义反序列化感谢这篇文章,使用GSON解析数组与多种类型,也是这篇文章,java集合- keyset() vs entrySet()在地图我所做的是创建这个自定义反序列化器:

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

顺便说一句,AccountRepresentative是一个内部类,AccountRepresentative中的两个数组总是在JSON中,只有属性是动态的。

我认为我将无法解析内部的一个,并为AccountDetailModel本身创建自定义的反序列化器,但我尝试为AccountRepresentative创建反序列化器,并且它很好:)

相关内容

  • 没有找到相关文章

最新更新