简体   繁体   中英

How to get Nested Gson keys (not values of keys) from Hashmap

My requirement is collect both the keys and values of keys from json and print it in list view as key value,so there will be no pre defined keys.

I have a nested object called as ListDetailModel, in that there are several other objects of other Pojo classes.

Right now I am able to derive the keys of parent object that is ListDetailModel.

My question is how to derive the keys of nested Pojos from the same entry set object. What I know is I can make separate objects for the required Pojo classes and derive keys.But can I do it from same class?

Map<String,ListDetailModel> result = gson.fromJson(response , Map.class);                                        //result.get("").getExcavatorInformation().getClass()
//noinspection HardCodedStringLiteral

ListDetailModel model = new ListDetailModel();
for (Map.Entry<String,ListDetailModel> entry : result.entrySet()) {
    String key = entry.getKey();
    Log.d("++++++++++++++"," " +key);
    // do stuff
}

So lets say there is another CarDetailModel inside ListDetailModel, cannot I do something like entry.get("CarDetailModel").getKey() and retrieve those keys ?

Map.Entry<String,ListDetailModel> is a node of the Map<String,ListDetailModel> .

You can only call getKey() or getValue() on it, not get("CarDetailModel") .

If you want to query a value in a Map by Key, then you have to call the function get(Key k) on the map itself, not on one of its Entries

Use GSON by Google rather than parsing yourself

For Demos: https://github.com/google/gson

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM