26

我的 JSON 文件看起来像

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

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

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

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

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

这是我的 FeatureDetails 模型,

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”。

4

4 回答 4

35

尝试这个:

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

当你浏览你的哈希图时,这样做:

for (Map.Entry<String, FeatureDetails> entry : featuresFromJson.entrySet()) {
                    FeatureDetails featureDetails = entry.getValue();
}
于 2013-11-05T19:06:28.397 回答
6

您看到这个的原因是因为您告诉 GSON 使用HashMap行中的结构反序列化 JSON 结构

... = new Gson().fromJson(JSONFeatureSet, HashMap.class);
                                          ^^
                                          Right here

因此,GSON 不知道 JSON 中的子对象不是简单的键值对,即使该结构可能与您的FeatureDetails对象的结构相匹配。

一种解决方案是创建一个包装FeatureDetails对象的模型,该模型将充当整个结构的根。这个对象可能看起来像这样:

public class FeatureDetailsRoot{
    private FeatureDetails SUBS_UID; // poor naming, but must match the key in your JSON
}

最后,您将通过该模型的课程:

= new Gson().fromJson(JSONFeatureSet, FeatureDetailsRoot.class)

更新

在回答您在评论中关于添加/拥有多个FeatureDetails对象的能力的问题时,目前的问题是您的 JSON 没有反映这种结构。意思是,"SUBS_UID"关键点指向单个对象,而不是数组对象。如果您想拥有这种能力,那么您的 json 将需要更改,以便它显示一个对象数组,如下所示:

{
    "SUBS_UID" : [{
       "featureSetName" : "Feature set name #1",
       ...attributes for feature #1
     },
     {
       "featureSetName" : "Feature set name #2",
       ...attributes for feature #2
     },
     ...other features
     ]
}

然后您可以简单地更改根类,使其包含FeatureDetails对象列表,如下所示:

public class FeatureDetailsRoot{
    private List<FeatureDetails> SUBS_UID;
}

让我知道这是否有意义(或者我是否误解了你)

于 2013-11-05T19:06:37.920 回答
0
(objectName as Map<String, Any>).get("fieldName")
于 2020-09-25T08:04:25.710 回答
0

代码在 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 代替 Map

于 2018-06-14T10:07:43.490 回答