我有以下JSON
{ "users": [
{
"id": 1,
"name": "Ash",
"email": "ashketchum@kanto.com"
},
{
"id": 2,
"name": "Gary",
"email": "garyoak@kanto.com"
}],
"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
} ] }
我如何在Java中将其反序列化为一个同时包含users数据数组和pokemon数据数组的对象或数组?最好是gson,但我不反对改变库。
2条答案
按热度按时间5jdjgkvh1#
添加POJO类:
然后调用
Example e = new Gson().fromJson(json, Example.class);
bvpmtnay2#
我建议使用Jackson将JSON字符串读取到Java Object中,而无需定义合适的类。
输出将是: