com.google.gson.internal.LinkedHashTreeMap不能转换为我的对象 [英] com.google.gson.internal.LinkedHashTreeMap cannot be cast to my object

查看:2182
本文介绍了com.google.gson.internal.LinkedHashTreeMap不能转换为我的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON文件看起来像

I have JSON file looks like

{
    "SUBS_UID" : {
        "featureSetName" : "SIEMENSGSMTELEPHONY MULTISIM",
        "featureName" : "MULTISIMIMSI",
        "featureKey" : [{
                "key" : "SCKEY",
                "valueType" : 0,
                "value" : "0"
            }
        ]
    },
}

所以关键是一个字符串SUBS_ID,该值是一个名为FeatureDetails的模型,其中包含属性featureSetName,featureName,...。
所以我使用google.json库读取JSON文件,像这样,

So the key is a String "SUBS_ID" and the value is a model called FeatureDetails which contains attributes "featureSetName,featureName,...". So i read from the JSON file using google.json lib like this,

HashMap<String, FeatureDetails> featuresFromJson = new Gson().fromJson(JSONFeatureSet, HashMap.class);

然后我试着循环这个HashMap获取值并将其转换为我的FeatureDetails模型, (Map.Entry entry:featuresFromJson.entrySet()){
featureDetails =(FeatureDetails)entry.getValue

then I'm trying to loop over this HashMap getting the value and cast it to my FeatureDetails model,

for (Map.Entry entry : featuresFromJson.entrySet()) {
                    featureDetails = (FeatureDetails) entry.getValue();
                }

这里是我的FeatureDetails模型,

and here is my FeatureDetails Model,

public class FeatureDetails {

    private String featureSetName;
    private String featureName;
    private ArrayList<FeatureKey> featureKey;
    private String groupKey;
    private String groupValue;

    public FeatureDetails() {
        featureKey =  new ArrayList<FeatureKey>();
    }

    public ArrayList<FeatureKey> getFeatureKey() {
        return featureKey;
    }

    public void setFeatureKey(ArrayList<FeatureKey> featureKey) {
        this.featureKey = featureKey;
    }

    public String getGroupKey() {
        return groupKey;
    }

    public void setGroupKey(String groupKey) {
        this.groupKey = groupKey;
    }

    public String getGroupValue() {
        return groupValue;
    }

    public void setGroupValue(String groupValue) {
        this.groupValue = groupValue;
    }

    public String getFeatureName() {
        return featureName;
    }

    public void setFeatureName(String featureName) {
        this.featureName = featureName;
    }

    public String getFeatureSetName() {
        return featureSetName;
    }

    public void setFeatureSetName(String featureSetName) {
        this.featureSetName = featureSetName;
    }
} 

但我得到一个异常com.google.gson .internal.LinkedHashTreeMap不能转换为com.asset.vsv.models.FeatureDetail。

but i got an exception "com.google.gson.internal.LinkedHashTreeMap cannot be cast to com.asset.vsv.models.FeatureDetail".

推荐答案

试试这个:

HashMap<String, FeatureDetails> featuresFromJson = new Gson().fromJson(JSONFeatureSet, new TypeToken<Map<String, FeatureDetails>>() {}.getType());

当你通过哈希映射时,请执行以下操作:
$ b (Map.Entry< String,FeatureDetails>条目:featuresFromJson.entrySet()){
FeatureDetails featureDetails = entry.getValue(); $ b

and when you going through your hash map do this:

for (Map.Entry<String, FeatureDetails> entry : featuresFromJson.entrySet()) {
                    FeatureDetails featureDetails = entry.getValue();
}

这篇关于com.google.gson.internal.LinkedHashTreeMap不能转换为我的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆