Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have JSON file looks like

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

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
394 views
Welcome To Ask or Share your Answers For Others

1 Answer

try this:

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

and when you going through your hash map do this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...