首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >com.google.gson.internal.LinkedHashTreeMap不能强制转换为我的对象

com.google.gson.internal.LinkedHashTreeMap不能强制转换为我的对象
EN

Stack Overflow用户
提问于 2013-11-06 02:55:33
回答 3查看 51.4K关注 0票数 26

我有一个JSON文件,看起来像

代码语言:javascript
复制
{
    "SUBS_UID" : {
        "featureSetName" : "SIEMENSGSMTELEPHONY MULTISIM",
        "featureName" : "MULTISIMIMSI",
        "featureKey" : [{
                "key" : "SCKEY",
                "valueType" : 0,
                "value" : "0"
            }
        ]
    },
}

因此,关键字是一个字符串"SUBS_ID“,值是一个名为FeatureDetails的模型,它包含属性"featureSetName,featureName,...”。所以我使用google.json库读取JSON文件,如下所示,

代码语言:javascript
复制
HashMap<String, FeatureDetails> featuresFromJson = new Gson().fromJson(JSONFeatureSet, HashMap.class);

然后我尝试遍历这个获取值的HashMap并将其转换为我的FeatureDetails模型,

代码语言:javascript
复制
for (Map.Entry entry : featuresFromJson.entrySet()) {
                    featureDetails = (FeatureDetails) entry.getValue();
                }

这是我的FeatureDetails模型,

代码语言:javascript
复制
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“。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-06 03:06:28

试试这个:

代码语言:javascript
复制
HashMap<String, FeatureDetails> featuresFromJson = new Gson().fromJson(JSONFeatureSet, new TypeToken<Map<String, FeatureDetails>>() {}.getType());

当您遍历您的哈希图时,请执行以下操作:

代码语言:javascript
复制
for (Map.Entry<String, FeatureDetails> entry : featuresFromJson.entrySet()) {
                    FeatureDetails featureDetails = entry.getValue();
}
票数 35
EN

Stack Overflow用户

发布于 2018-06-14 18:07:43

代码在Kotlin中:使用val type = object : TypeToken<HashMap<String, FoodLogEntry>>() {}.type Gson().fromJson(dataStr, type)

代替val type = object : TypeToken<Map<String, FoodLogEntry>>() {}.type Gson().fromJson(dataStr, type)注意:用HashMap代替映射

票数 0
EN

Stack Overflow用户

发布于 2020-09-25 16:04:26

代码语言:javascript
复制
(objectName as Map<String, Any>).get("fieldName")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19796590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档