我目前正在開展關於Golang的教育項目,並使用Steam RESTAPI來檢索玩家的CounterStrike(CS:GO)庫存。與JSON一起使用Steam Rest API
花費大量的時間後,這是我已經能夠獲得進展:
1)調用包含ID返回JSON消息,他們的庫存用戶的URL的請求(但JSON是有點棘手這就是我遇到的麻煩)。
2)我需要遍歷JSON的rgDescriptions部分。
3)我已經構建了下面的代碼(位於低了下去)穿過rgDescription循環,但是我不確定如何再得到它裏面一切的價值。
代碼:
var data map[string]interface{}
r, err := myClient.Get("http://steamcommunity.com/profiles/" + id + "/inventory/json/730/2")
detectErr(err)
defer r.Body.Close()
result, err := ioutil.ReadAll(r.Body)
detectErr(err)
err = json.Unmarshal(result, &data)
detectErr(err)
myMap := data["rgDescriptions"]
v := reflect.ValueOf(myMap)
for _, key := range v.MapKeys() {
value := v.MapIndex(key)
fmt.Println(key, "-", value)
//fmt.Println(reflect.ValueOf(value).MapIndex(varr[0])) // My idea for making this entire thing work, however it just spits out an error instead. :(
fmt.Println("----------------\n")
}
上面的代碼打印出的線沿線的東西:
1432174707_0 - map[classid:1432174707 market_hash_name:Revolver Case background_color: market_tradable_restriction:7 tags:[map[category_name:Type internal_name:CSGO_Type_WeaponCase name:Container category:Type] map[name:The Revolver Case Collection category:ItemSet category_name:Collection internal_name:set_community_10] map[internal_name:normal name:Normal category:Quality category_name:Category] map[category:Rarity color:b0c3d9 category_name:Quality internal_name:Rarity_Common name:Base Grade]] appid:730 instanceid:0 icon_url:-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFYwnfKfcG9HvN7iktaOkqD1auLTxD5SvZYgiLvFpo7xjVLh-kdrYWnzcoGLMlhpsyM-5vg market_name:Revolver Case type:Base Grade Container marketable:1 commodity:1 icon_drag_url: name:Revolver Case name_color:D2D2D2 tradable:1 descriptions:[map[type:html value: ] map[type:html value:Container Series #111 color:99ccff] map[type:html value: ] map[type:html value:Contains one of the following:] map[type:html value:R8 Revolver | Crimson Web color:4b69ff] map[type:html value:AUG | Ricochet color:4b69ff] map[type:html value:Desert Eagle | Corinthian color:4b69ff] map[value:P2000 | Imperial color:4b69ff type:html] map[type:html value:Sawed-Off | Yorick color:4b69ff] map[color:4b69ff type:html value:SCAR-20 | Outbreak] map[type:html value:PP-Bizon | Fuel Rod color:8847ff] map[type:html value:Five-SeveN | Retrobution color:8847ff] map[type:html value:Negev | Power Loader color:8847ff] map[type:html value:SG 553 | Tiger Moth color:8847ff] map[type:html value:Tec-9 | Avalanche color:8847ff] map[type:html value:XM1014 | Teclu Burner color:8847ff] map[type:html value:AK-47 | Point Disarray color:d32ce6] map[type:html value:G3SG1 | The Executioner color:d32ce6] map[type:html value:P90 | Shapewood color:d32ce6] map[type:html value:M4A4 | Royal Paladin color:eb4b4b] map[type:html value:R8 Revolver | Fade color:eb4b4b] map[value:or an Exceedingly Rare Special Item! color:ffd700 type:html] map[type:html value: ] map[color:00a000 app_data:map[limited:1] type:html value:]]]
如果你想看到我在這裏接受JSON的例子是示例網址。 http://steamcommunity.com/profiles/76561198096365603/inventory/json/730/2
所以最後,我需要弄清楚如何遍歷值變量的內容。
任何幫助將不勝感激。 謝謝。
爲什麼不解開模型化JSON響應結構的結構?這將是一種更簡單的訪問數據的方式。 –