简体   繁体   English

Java使用gson解析复杂的json响应

[英]Java using gson to parse a complex json response

I need to extract values from this complex json response using gson: 我需要使用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
      }
    ]

  }
}

I've done some searching here on stackoverflow and on google, but I was unable to find any easily understandable examples that show how to traverse complex json objects such as this in java. 我在这里做了一些关于stackoverflow和google的搜索,但是我找不到任何易于理解的示例来说明如何遍历复杂的json对象,例如在Java中。 As you can see, the amount of nesting that may appear in the response is a bit overwhelming. 如您所见,响应中可能出现的嵌套数量有点不堪重负。 I know that I have to create a class (or even classes?) to hold the data, and use fromJson() to populate it, but I am unsure of exactly how it should be set up, as well as how to handle the large "items" array. 我知道我必须创建一个类(甚至是类?)来保存数据,并使用fromJson()来填充数据,但是我不确定应该如何设置以及如何处理大型数据。 “项目”数组。 Any hints/tips/examples would be much appreciated, thanks. 任何提示/技巧/示例将不胜感激,谢谢。 :) :)

PS I'm new to stackoverflow and fairly new to Java, please go easy on me. PS我是stackoverflow的新手,而Java的新手,请放轻松。

first all create class that represents your JSON String. 首先创建class ,代表您的JSON字符串。

After, using GSON library convert your String to class Object. 之后,使用GSON库将您的String转换为Object class Here is example: 这是示例:

your main class lets call MyRoot . 您的主类可以调用MyRoot In MyRoot you have 1 filed named result so we build Result class. MyRoot您有1个命名为result MyRoot ,因此我们构建Result类。

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

The items should be an ArrayList of Objects (lets call them Item ) 这些items应该是ArrayList of Objects(我们称它们为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;
}

When you done just run: 完成后,只需运行:

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

And now you have class MyRoot with all content. 现在,您拥有了包含所有内容的MyRoot类。 By the way, add get/set for every field. 顺便说一句,为每个字段添加获取/设置。

You can use this source for info 您可以使用此来源获取信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM