我需要使用 gson 从这个复杂的 json 响应中提取值:
{
"result": {
"status": 1,
"num_backpack_slots": 500,
"items": [
{
"id": 455391,
"original_id": 455391,
"defindex": 125,
"level": 1,
"quality": 6,
"inventory": 2147483742,
"quantity": 1,
"origin": 0,
"flag_cannot_trade": true
},
{
"id": 513820071,
"original_id": 513820071,
"defindex": 471,
"level": 50,
"quality": 6,
"inventory": 2147483741,
"quantity": 1,
"origin": 13,
"flag_cannot_trade": true
},
{
"id": 576746588,
"original_id": 570246654,
"defindex": 39,
"level": 10,
"quality": 11,
"inventory": 2147483720,
"quantity": 1,
"origin": 8,
"equipped": [
{
"class": 7,
"slot": 1
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 1000,
"float_value": 0.000000
}
]
},
{
"id": 576775968,
"original_id": 418597987,
"defindex": 40,
"level": 10,
"quality": 11,
"inventory": 2147483719,
"quantity": 1,
"origin": 3,
"equipped": [
{
"class": 7,
"slot": 0
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 4394,
"float_value": 0.000000
}
]
},
{
"id": 607610629,
"original_id": 470196433,
"defindex": 141,
"level": 5,
"quality": 11,
"inventory": 2147483710,
"quantity": 1,
"origin": 8,
"equipped": [
{
"class": 9,
"slot": 0
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 352,
"float_value": 0.000000
}
]
},
{
"id": 658784889,
"original_id": 367005538,
"defindex": 61,
"level": 5,
"quality": 11,
"inventory": 2147483712,
"quantity": 1,
"origin": 3,
"equipped": [
{
"class": 8,
"slot": 1
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 214,
"float_value": 0.000000
}
]
},
{
"id": 697046877,
"original_id": 697046877,
"defindex": 242,
"level": 1,
"quality": 6,
"inventory": 2147483739,
"quantity": 1,
"origin": 9,
"flag_cannot_trade": true,
"attributes": [
{
"defindex": 185,
"value": 1328492055,
"float_value": 2939033344.000000
}
]
},
{
"id": 697206938,
"original_id": 697206938,
"defindex": 581,
"level": 31,
"quality": 6,
"inventory": 2147483740,
"quantity": 1,
"origin": 1,
"flag_cannot_trade": true
},
{
"id": 708417260,
"original_id": 688333621,
"defindex": 37,
"level": 10,
"quality": 11,
"inventory": 2147483711,
"quantity": 1,
"origin": 8,
"equipped": [
{
"class": 5,
"slot": 2
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 131,
"float_value": 0.000000
}
]
},
{
"id": 708425760,
"original_id": 597434022,
"defindex": 58,
"level": 5,
"quality": 11,
"inventory": 2147483731,
"quantity": 1,
"origin": 8,
"equipped": [
{
"class": 2,
"slot": 1
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 540,
"float_value": 0.000000
}
]
},
{
"id": 735058153,
"original_id": 702208573,
"defindex": 701,
"level": 84,
"quality": 6,
"inventory": 2147483746,
"quantity": 1,
"origin": 4,
"attributes": [
{
"defindex": 228,
"value": 44044472,
"float_value": 0.000000,
"account_info": {
"steamid": 76561198004310200,
"personaname": "Bukz | TF2calc.tk"
}
}
]
},
{
"id": 783535692,
"original_id": 746181220,
"defindex": 239,
"level": 10,
"quality": 11,
"inventory": 2147483732,
"quantity": 1,
"origin": 8,
"equipped": [
{
"class": 6,
"slot": 2
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 2,
"float_value": 0.000000
}
]
},
{
"id": 790390696,
"original_id": 705188110,
"defindex": 56,
"level": 10,
"quality": 6,
"inventory": 2147483840,
"quantity": 1,
"origin": 0
},
{
"id": 823322632,
"original_id": 663311596,
"defindex": 167,
"level": 5,
"quality": 6,
"inventory": 2147483748,
"quantity": 1,
"origin": 0,
"equipped": [
{
"class": 3,
"slot": 9
},
{
"class": 4,
"slot": 9
},
{
"class": 1,
"slot": 9
},
{
"class": 2,
"slot": 9
},
{
"class": 7,
"slot": 9
},
{
"class": 5,
"slot": 9
},
{
"class": 6,
"slot": 9
},
{
"class": 8,
"slot": 9
},
{
"class": 9,
"slot": 9
}
]
},
{
"id": 831931223,
"original_id": 794619497,
"defindex": 140,
"level": 5,
"quality": 6,
"inventory": 2147483831,
"quantity": 1,
"origin": 0,
"equipped": [
{
"class": 9,
"slot": 1
}
]
},
{
"id": 838987757,
"original_id": 151360199,
"defindex": 264,
"level": 5,
"quality": 6,
"inventory": 2147483800,
"quantity": 1,
"origin": 3,
"equipped": [
{
"class": 7,
"slot": 2
},
{
"class": 3,
"slot": 2
},
{
"class": 2,
"slot": 2
},
{
"class": 1,
"slot": 2
}
]
},
{
"id": 839449369,
"original_id": 763635464,
"defindex": 477,
"level": 5,
"quality": 6,
"inventory": 2147483747,
"quantity": 1,
"origin": 0
},
{
"id": 839531073,
"original_id": 482419131,
"defindex": 211,
"level": 1,
"quality": 11,
"inventory": 2147483709,
"quantity": 1,
"origin": 3,
"equipped": [
{
"class": 5,
"slot": 1
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 116,
"float_value": 0.000000
},
{
"defindex": 294,
"value": 325,
"float_value": 0.000000
}
]
},
{
"id": 843856946,
"original_id": 93370304,
"defindex": 143,
"level": 42,
"quality": 6,
"inventory": 2147483700,
"quantity": 1,
"origin": 3
},
{
"id": 973205893,
"original_id": 864931488,
"defindex": 522,
"level": 10,
"quality": 6,
"inventory": 2147483726,
"quantity": 1,
"origin": 0,
"equipped": [
{
"class": 3,
"slot": 10
},
{
"class": 2,
"slot": 10
},
{
"class": 1,
"slot": 10
},
{
"class": 6,
"slot": 10
},
{
"class": 5,
"slot": 10
},
{
"class": 7,
"slot": 10
}
]
},
{
"id": 975592060,
"original_id": 975592060,
"defindex": 744,
"level": 69,
"quality": 6,
"inventory": 2147483743,
"quantity": 1,
"origin": 1,
"flag_cannot_trade": true
},
{
"id": 986693653,
"original_id": 643535744,
"defindex": 474,
"level": 25,
"quality": 6,
"inventory": 2147483799,
"quantity": 1,
"origin": 0,
"attributes": [
{
"defindex": 152,
"value": 4142075573,
"float_value": -2302636095545453300000000000000000.000000
},
{
"defindex": 227,
"value": 146855095,
"float_value": 0.000000
}
]
},
{
"id": 986693656,
"original_id": 699128493,
"defindex": 197,
"level": 1,
"quality": 11,
"inventory": 2147483722,
"quantity": 1,
"origin": 8,
"custom_name": "MAXIMUM NOOB HEAT",
"equipped": [
{
"class": 9,
"slot": 2
}
]
,
"attributes": [
{
"defindex": 189,
"value": 1093664768,
"float_value": 11.000000
},
{
"defindex": 214,
"value": 218,
"float_value": 0.000000
},
{
"defindex": 294,
"value": 18,
"float_value": 0.000000
}
]
},
{
"id": 988633074,
"original_id": 253598540,
"defindex": 118,
"level": 21,
"quality": 3,
"inventory": 2147483738,
"quantity": 1,
"origin": 3
},
{
"id": 1025190520,
"original_id": 346911280,
"defindex": 483,
"level": 15,
"quality": 6,
"inventory": 2147483717,
"quantity": 1,
"origin": 3,
"equipped": [
{
"class": 8,
"slot": 10
}
]
,
"attributes": [
{
"defindex": 142,
"value": 1266641332,
"float_value": 16738740.000000
},
{
"defindex": 261,
"value": 1266641332,
"float_value": 16738740.000000
}
]
},
{
"id": 1025190651,
"original_id": 677752195,
"defindex": 142,
"level": 15,
"quality": 6,
"inventory": 2147483830,
"quantity": 1,
"origin": 0
},
{
"id": 1025191043,
"original_id": 244038290,
"defindex": 237,
"level": 1,
"quality": 6,
"inventory": 2147483801,
"quantity": 1,
"origin": 3
},
{
"id": 1025191147,
"original_id": 522190433,
"defindex": 265,
"level": 1,
"quality": 6,
"inventory": 2147483802,
"quantity": 1,
"origin": 4
},
{
"id": 1228670494,
"original_id": 1228670494,
"defindex": 311,
"level": 1,
"quality": 6,
"inventory": 2147483835,
"quantity": 1,
"origin": 0
},
{
"id": 1228702072,
"original_id": 1228702072,
"defindex": 5022,
"level": 10,
"quality": 6,
"inventory": 2147483690,
"quantity": 1,
"origin": 0,
"attributes": [
{
"defindex": 187,
"value": 1111228416,
"float_value": 47.000000
}
]
},
{
"id": 1228702073,
"original_id": 1228702073,
"defindex": 5627,
"level": 10,
"quality": 6,
"inventory": 2147483689,
"quantity": 1,
"origin": 0,
"attributes": [
{
"defindex": 187,
"value": 1112276992,
"float_value": 51.000000
}
]
},
{
"id": 1228758422,
"original_id": 1228758422,
"defindex": 648,
"level": 15,
"quality": 6,
"inventory": 2147483825,
"quantity": 1,
"origin": 0
},
{
"id": 1228758423,
"original_id": 1228758423,
"defindex": 5611,
"level": 1,
"quality": 6,
"inventory": 2147483688,
"quantity": 1,
"origin": 0
},
{
"id": 1228897312,
"original_id": 1228897312,
"defindex": 173,
"level": 5,
"quality": 6,
"inventory": 2147483815,
"quantity": 1,
"origin": 0
},
{
"id": 1228897313,
"original_id": 1228897313,
"defindex": 5615,
"level": 1,
"quality": 6,
"inventory": 2147483687,
"quantity": 1,
"origin": 0
}
]
}
}
我已经在 stackoverflow 和 google 上进行了一些搜索,但我找不到任何易于理解的示例来展示如何在 java 中遍历复杂的 json 对象,例如 this。正如您所看到的,响应中可能出现的嵌套数量有点让人不知所措。我知道我必须创建一个类(甚至类?)来保存数据,并使用 fromJson() 来填充它,但我不确定它应该如何设置,以及如何处理大“项目”数组。任何提示/提示/示例将不胜感激,谢谢。:)
PS我是stackoverflow的新手,对Java也很陌生,请放轻松。