I have the following json
{ "users": [
{
"id": 1,
"name": "Ash",
"email": "[email protected]"
},
{
"id": 2,
"name": "Gary",
"email": "[email protected]"
}],
"pokemon": [
{
"userId": 1,
"name": "Ember",
"frontImg": "/images/pokemon/local-mon/ember/front.png",
"backImg": "/images/pokemon/local-mon/ember/back.png",
"type": "fire",
"hp": 100,
"id": 1
},
{
"userId": 1,
"name": "Draggy",
"frontImg": "/images/pokemon/local-mon/dragon/front.png",
"backImg": "/images/pokemon/local-mon/dragon/back.png",
"type": "dragon",
"hp": 100,
"id": 2
} ] }
How could I deserialize this into a single object or array containing both the users array of data and the pokemon array of data in Java? Preferably in gson but I’m not opposed to changing libraries.
2
Answers
Add the POJO classes:
And then call
Example e = new Gson().fromJson(json, Example.class);
I suggest using Jackson to read JSON strings into Java Object without defining a suitable class.
The output will be: