2

我需要使用 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也很陌生,请放轻松。

4

1 回答 1

1

首先创建class代表您的 JSON 字符串。

之后,使用 GSON 库将您的字符串转换为class对象。这是示例:

你的主要课程让我们打电话MyRoot。在MyRoot您有 1 个已命名的文件中result,因此我们构建了Result类。

public class MyRoot{ // <- you can give your class name,
   private Result result = 0; // Its importent to provide the same field name
}

items应该是一个对象(让我们ArrayList称之为Item

在此处输入图像描述

public class Result{
   private int status = 0;
   private int num_backpack_slots = 0;
   private List<Item> items = null; 
}


public class Item{
   private int id = 0;
   private in toriginal_id = 0;
   private int defindex = 0;
   private int level = 0;
   private int quality = 0;
   private int inventory = 0;
   private int quantity = 0;
   private int origin = 0;
   private boolean flag_cannot_trade = true;
   private List<Equipped> equipped = null;
   private List<Attributes> attributes = null;
   private boolean flag_cannot_trade = false;
}

public class Equipped{
   //private int class = 0; // here json has 'class' field , you cant use this name, use SerializedName
    @SerializedName("class") private  int myclass = 0;
   private int slot = 0
}

public class Attributes{
   private int defindex = 0; 
   private int value = 0;
   private doublefloat_value= 0;
}

完成后运行:

Gson mGson = new Gson(); 
MyRoot response = mGson.fromJson(jsonString, MyRoot.class); 

现在你有了包含所有内容的 MyRoot 类。顺便说一句,为每个字段添加 get/set。

您可以使用此来源获取信息

于 2012-12-02T05:55:13.047 回答