第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何格式化具有多個(gè)對(duì)象返回的 json 結(jié)構(gòu)?(動(dòng)態(tài)的)

如何格式化具有多個(gè)對(duì)象返回的 json 結(jié)構(gòu)?(動(dòng)態(tài)的)

Go
慕的地6264312 2021-11-01 17:03:33
我有一個(gè) API 調(diào)用結(jié)果可以有多個(gè)返回我想知道結(jié)構(gòu)應(yīng)該是什么樣子,這是我到目前為止所擁有的,但它返回什么也沒(méi)有。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"`}如果有人能告訴我我的結(jié)構(gòu)有什么問(wèn)題,那就太感謝了。這混淆我是怎么樣描述的回報(bào)不是可以范圍從0-20的數(shù)組,但多個(gè)對(duì)象,我怎么準(zhǔn)備這一個(gè)結(jié)構(gòu)時(shí),我不知道有多少個(gè)對(duì)象要回報(bào)的東西,以及result可以返回多個(gè)"720616831"那么這應(yīng)該怎么看?
查看完整描述

2 回答

?
FFIVE

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

你會(huì)因?yàn)榈谝粋€(gè)錯(cuò)誤而自責(zé)——你的 JSON 有,result但你的 struct 標(biāo)簽有response.


第二個(gè)問(wèn)題比較棘手。問(wèn)題是您聲明您的Asset地圖作為名為“資產(chǎn)”的鍵嵌套在結(jié)果中,但事實(shí)并非如此。其實(shí)它只是所有結(jié)果的按鍵其他比成功/錯(cuò)誤。不幸的是,encoding/json沒(méi)有任何方式來(lái)表達(dá)這一點(diǎn)。你可以說(shuō)它result是 a map[string]interface{},然后成功/錯(cuò)誤(如果它們存在)將是 bool/string,并且資產(chǎn)將更多map[string]interface{}s 包含所有其他字段的鍵。這是可行的,但它有點(diǎn)丑陋/效率低下,如果您想轉(zhuǎn)換為適當(dāng)?shù)慕Y(jié)構(gòu)類型以便您可以在其上擁有方法,則需要做很多工作。


也許更好的方法是解碼成這種類型:


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 AssetIntermediate

err := 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調(diào)用要多得多,但它基本上應(yīng)該做正確的事情。


如果所有這些都在一個(gè)返回的函數(shù)中,(*AssetInfo, error)那么正確的替代方法/* Handle err */可能是


if err != nil {

    return nil, err

}


查看完整回答
反對(duì) 回復(fù) 2021-11-01
  • 2 回答
  • 0 關(guān)注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)