使用 Steam Rest API 和 JSON
Using Steam Rest API with JSON
我目前正在开展一个关于 Golang 的教育项目,并使用 Steam RESTAPI 检索玩家的反恐精英 (CS:GO) 库存。
在花费了大量时间后,这是我取得的进步:
1) 向包含用户 ID returns 的 url 调用带有他们库存的 JSON 消息的请求(但是 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 的示例,我这里是一个示例 URL。 http://steamcommunity.com/profiles/76561198096365603/inventory/json/730/2
所以总而言之,我需要弄清楚如何循环遍历值变量的内容。
如有任何帮助,我们将不胜感激。
谢谢。
下面是一个工作示例。关键是声明一个与您的 JSON.
相匹配的结构类型
您需要为项目使用地图类型,因为密钥不一致。
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Inventory struct {
Success bool `json:"success"`
Items map[string]*Item `json:"rgInventory"`
Descriptions map[string]*Description `json:"rgDescriptions"`
}
type Item struct {
ID string `json:"id"`
ClassID string `json:"classid"`
InstanceID string `json:"instanceid"`
Amount string `json:"amount"`
Pos int `json:"pos"`
}
type Description struct {
Descriptions []struct {
Value string `json:"value"`
} `json:"descriptions"`
}
func main() {
res, err := http.Get("http://steamcommunity.com/profiles/76561198096365603/inventory/json/730/2")
if err != nil {
log.Fatal(err)
}
inventory := &Inventory{}
if err := json.NewDecoder(res.Body).Decode(&inventory); err != nil {
log.Fatal(err)
}
for _, item := range inventory.Descriptions {
for _, d := range item.Descriptions {
fmt.Printf(d.Value)
}
}
}
我目前正在开展一个关于 Golang 的教育项目,并使用 Steam RESTAPI 检索玩家的反恐精英 (CS:GO) 库存。
在花费了大量时间后,这是我取得的进步:
1) 向包含用户 ID returns 的 url 调用带有他们库存的 JSON 消息的请求(但是 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 的示例,我这里是一个示例 URL。 http://steamcommunity.com/profiles/76561198096365603/inventory/json/730/2
所以总而言之,我需要弄清楚如何循环遍历值变量的内容。
如有任何帮助,我们将不胜感激。 谢谢。
下面是一个工作示例。关键是声明一个与您的 JSON.
相匹配的结构类型您需要为项目使用地图类型,因为密钥不一致。
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Inventory struct {
Success bool `json:"success"`
Items map[string]*Item `json:"rgInventory"`
Descriptions map[string]*Description `json:"rgDescriptions"`
}
type Item struct {
ID string `json:"id"`
ClassID string `json:"classid"`
InstanceID string `json:"instanceid"`
Amount string `json:"amount"`
Pos int `json:"pos"`
}
type Description struct {
Descriptions []struct {
Value string `json:"value"`
} `json:"descriptions"`
}
func main() {
res, err := http.Get("http://steamcommunity.com/profiles/76561198096365603/inventory/json/730/2")
if err != nil {
log.Fatal(err)
}
inventory := &Inventory{}
if err := json.NewDecoder(res.Body).Decode(&inventory); err != nil {
log.Fatal(err)
}
for _, item := range inventory.Descriptions {
for _, d := range item.Descriptions {
fmt.Printf(d.Value)
}
}
}