2012-12-02 103 views
2

我需要使用GSON提取從這個複雜的JSON響應值:Java中使用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 
     } 
    ] 

    } 
} 

我已經做了一些搜索這裏計算器和谷歌,但我無法找到任何易於理解的示例,展示如何遍歷java中的複雜json對象。正如你所看到的,響應中可能出現的嵌套數量有點令人難以置信。我知道我必須創建一個類(甚至是類)來保存數據,並使用fromJson()來填充它,但我不確定應該如何設置它,以及如何處理大「項目」數組。任何提示/技巧/例子將不勝感激,謝謝。 :)

P.S.我是新來的stackoverflow和相當新的Java,請容易對我。

回答

1

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

之後,使用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

enter image description here

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。

您可以使用此source for info

+0

我不知道如何設置了類這個複雜的響應是我的問題。我已經看到和理解更簡單的例子。 – Bukz

+0

現在我寫你如何做到這一點。耐心:) –

+0

我明白了,這很有道理。我會從這裏看到我能做些什麼。謝謝你的快速反應。 :) – Bukz