如何格式化具有多个对象返回的 json 结构?(动态的)

我有一个 API 调用

结果可以有多个返回我想知道结构应该是什么样子,这是我到目前为止所拥有的,但它返回什么也没有。


type AssetInfo struct {

    Result `json:"result"`

}


type Result struct {

    Asset   map[string]Asset `json:"asset"`

    Success bool             `json:"success,omitempty"`

}


type Asset struct {

    IconUrl           string                   `json:"icon_url,omitempty"`

    IconUrlLarge      string                   `json:"icon_url_large,omitempty"`

    IconDragUrl       string                   `json:"icon_drag_url,omitempty"`

    Name              string                   `json:"name,omitempty"`

    MarketHashName    string                   `json:"market_hash_name,omitempty"`

    MarketName        string                   `json:"market_name,omitempty"`

    NameColor         string                   `json:"name_color,omitempty"`

    BGColor           string                   `json:"background_color,omitempty"`

    Type              string                   `json:"type,omitempty"`

    Tradable          string                   `json:"tradable,omitempty"`

    Marketable        string                   `json:"marketable,omitempty"`

    Commodity         string                   `json:"commodity,omitempty"`

    TradeRestrict     string                   `json:"market_tradeable_restriction,omitempty"`

    FraudWarnings     string                   `json:"fraudwarnings,omitempty"`

    Descriptions      map[string]*Descriptions `json:"descriptions,omitempty"`

    OwnerDescriptions string                   `json:"owner_descriptions,omitempty"`

    Tags              map[string]*Tags         `json:"tags,omitempty"`

    ClassId           string                   `json:"classid,omitempty"`

}


type Descriptions struct {

    Type    string `json:"type"`

    Value   string `json:"value"`

    Color   string `json:"color,omitempty"`

    AppData string `json:"appdata"`

}



如果有人能告诉我我的结构有什么问题,那就太感谢了。


这混淆我是怎么样描述的回报不是可以范围从0-20的数组,但多个对象,我怎么准备这一个结构时,我不知道有多少个对象要回报的东西,以及result可以返回多个"720616831"那么这应该怎么看?


慕的地6264312
浏览 155回答 2
2回答

FFIVE

你会因为第一个错误而自责——你的 JSON 有,result但你的 struct 标签有response.第二个问题比较棘手。问题是您声明您的Asset地图作为名为“资产”的键嵌套在结果中,但事实并非如此。其实它只是所有结果的按键其他比成功/错误。不幸的是,encoding/json没有任何方式来表达这一点。你可以说它result是 a map[string]interface{},然后成功/错误(如果它们存在)将是 bool/string,并且资产将更多map[string]interface{}s 包含所有其他字段的键。这是可行的,但它有点丑陋/效率低下,如果您想转换为适当的结构类型以便您可以在其上拥有方法,则需要做很多工作。也许更好的方法是解码成这种类型:type AssetIntermediate struct {    Result map[string]json.RawMessage `json:"result"`}以及拥有type Asset struct { /* all those fields */ }type AssetInfo struct {    Success bool    Error string    Assets map[string]Asset}然后你可以做var intermediate AssetIntermediateerr := json.Unmarshal(data, &intermediate)/* handle err */var ai AssetInfo/* At this point, intermediate.Result is a map * of strings to json.RawMessage, which is just a []byte * containing not-yet-decoded JSON. We want to take * each field and put it into ai where it belongs. */for k, v := range intermediate.Result {    var err error    // error and success keys are decoded into the respective fields    if k == "error" {        err = json.Unmarshal(v, &ai.Error)    } else if k == "success" {        err = json.Unmarshal(v, &ai.Success)    } else {        // Otherwise, we have an asset. First decode it...        var asset Asset        err = json.Unmarshal(v, &asset)        if err == nil {            // Create the Assets map if it doesn't exist yet            if ai.Assets == nil {                ai.Assets = map[string]Asset{}            }            // And store the asset in the map under the key k.            ai.Assets[k] = asset        }    }    /* handle err if non-nil */}这比一次Decode调用要多得多,但它基本上应该做正确的事情。如果所有这些都在一个返回的函数中,(*AssetInfo, error)那么正确的替代方法/* Handle err */可能是if err != nil {    return nil, err}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go