亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解碼這個json字符串

如何解碼這個json字符串

Go
眼眸繁星 2022-07-18 09:55:31
我的應用程序前端的 json 字符串是{"ticker":"glencore","dated":"25/5/2121","resource_name":"testing","latlong":"","type":"","measured":{"0":{"symbol":"4","grade":"4","grade_unit":"grams_per_ton","contained":"48000000","contained_unit":"t"},"1":{"symbol":"1","grade":"4","grade_unit":"grams_per_ton","contained":"48000000","contained_unit":"t"},"value":"5","unit":"tons"},"indicated":{"0":{"symbol":"4","grade":"1","grade_unit":"grams_per_ton","contained":"10000000","contained_unit":"t"},"1":{"symbol":"1","grade":"1","grade_unit":"grams_per_ton","contained":"10000000","contained_unit":"t"},"value":"4","unit":"tons"},"inferred":{"0":{"symbol":"4","grade":"2","grade_unit":"grams_per_ton","contained":"18000000","contained_unit":"t"},"1":{"symbol":"1","grade":"2","grade_unit":"grams_per_ton","contained":"18000000","contained_unit":"t"},"value":"3","unit":"tons"},"total":{"0":{"symbol":"4","grade":"3","grade_unit":"grams_per_ton","contained":"24000000","contained_unit":"t"},"1":{"symbol":"1","grade":"3","grade_unit":"grams_per_ton","contained":"24000000","contained_unit":"t"},"value":"2","unit":"tons"}}我要解碼的golang代碼是type HTTPMineral struct {        Symbol        string `json:"symbol"`        Grade         string `json:"grade"`        GradeUnit     string `json:"grade_unit"`        Contained     string `json:"contained"`        ContainedUnit string `json:"contained_unit"`    }    type HTTPResource struct {        Ticker        string        `json:"ticker"`        ResourceName  string        `json:"resource_name"`        DateOfRelease string        `json:"dated"`        Measured      []HTTPMineral `json:"measured"`        Indicated     []HTTPMineral `json:"indicated"`        Inferred      []HTTPMineral `json:"inferred"`        Total         []HTTPMineral `json:"total"`    }我不斷收到以下錯誤2020/11/30 16:36:34 unmarshaling error  json: cannot unmarshal object into Go struct field HTTPResource.measured of type []main.HTTPMineralDecodedresource我確定它一定是我忽略的東西,但我已經看過好幾次了,我不明白我的結構是如何不正確的。非常感謝幫助
查看完整描述

2 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

、等字段不是數組,它們是像數組一樣組織的 JSON 對象measuredindicated利用

Measured map[string]HTTPMineral

然后通過使用data.Measured["0"]而不是獲取項目data.Measured[0]


查看完整回答
反對 回復 2022-07-18
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

Key Indicated、Inferred 和 Total 不是數組類型,它們是關聯數組(以鍵名作為索引)。


  type HTTPMineral struct {

            Symbol        string `json:"symbol"`

            Grade         string `json:"grade"`

            GradeUnit     string `json:"grade_unit"`

            Contained     string `json:"contained"`

            ContainedUnit string `json:"contained_unit"`

        }

        type SHttpMineral map[string]HTTPMineral

        type PHttpMineral struct {

            IHttpMineral

            Value string `json:"value"`

            Unit  string `json:"unit"`

        }

    

        type HTTPResource struct {

            Ticker        string       `json:"ticker"`

            ResourceName  string       `json:"resource_name"`

            DateOfRelease string       `json:"dated"`

            Measured      PHttpMineral `json:"measured"`

            Indicated     PHttpMineral `json:"indicated"`

            Inferred      PHttpMineral `json:"inferred"`

            Total         PHttpMineral `json:"total"`

        }

    

        resource := &HTTPResource{}

        if err := json.NewDecoder(r.Body).Decode(resource); err != nil {

        log.Println("unmarshaling error ", err)

    }

fmt.Println("Decodedresource")


查看完整回答
反對 回復 2022-07-18
  • 2 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號