#Json to Java - how to access dynamically named inner object

7 messages · Page 1 of 1 (latest)

patent ledge
#

I have a json object of objects and I'm trying to map the inner objects to a Java class.

"176294012937458694583": {
        "id": "176294012937458694583",
        "description": "Beta",
        "name": "BetaName",
        "account_id": "096382648647"
}
"3428378574582548573293": {
        "id": "3428378574582548573293",
        "description": "Omega",
        "name": "OmegaName",
        "account_id": "32735847382"
}
"4657556334646766874556": {
    "id": "4657556334646766874556",
    "description": "Delta",
    "name": "DeltaName",
    "account_id": "93758598257"
}
}```

```MyJavaClass 
String id;
String description;
String name;
String accountId;```

I was hoping to access the inner objects like this & map to MyJavaClass using Gson:
 
```JsonObject jsonObject = response.body().getAsJsonObject("176294012937458694583");```

But the number of inner objects and the memberName are unknown prior to the data return. So what would be a better way to do this?
prime hedgeBOT
#

This post has been reserved for your question.

Hey @patent ledge! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rich brook
#

you could represent the top level object as a Map<String, MyJavaClass>

#

there might be a cleaner way to do that in the context of gson though

patent ledge
#

I ended up deserializing with Map.Entry<String, JsonElement> and was able to access inner classes with jsonObject.entrySet() before mapping with Gson. Thanks for the suggestion.

prime hedgeBOT