Get JSON key name using GSON

后端 未结 3 1751
名媛妹妹
名媛妹妹 2021-02-14 01:48

I have a JSON array which contains objects such as this:

{
    \"bjones\": {
        \"fname\": \"Betty\",
        \"lname\": \"Jones\",
        \"password\": \         


        
3条回答
  •  鱼传尺愫
    2021-02-14 02:46

    Use entrySet to get the keys. Loop through the entries and create a User for every key.

    JsonObject result = p.parse(file).getAsJsonObject();
    Set> entrySet = result.entrySet();
    for(Map.Entry entry : entrySet) {
        User newUser = gson.fromJson(p.getAsJsonObject(entry.getKey()), User.class);
        newUser.username = entry.getKey();
        //code...
    }
    

提交回复
热议问题