简体   繁体   中英

Java using gson to parse a complex json response

I need to extract values from this complex json response using gson:

{
  "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. 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. 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.

first all create class that represents your JSON String.

After, using GSON library convert your String to class Object. Here is example:

your main class lets call MyRoot . In MyRoot you have 1 filed named result so we build Result class.

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 )

在此处输入图片说明

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. By the way, add get/set for every field.

You can use this source for info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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