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

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

json Unmarshal 將屬性替換為零(如果它是在標簽中構建的)

json Unmarshal 將屬性替換為零(如果它是在標簽中構建的)

Go
撒科打諢 2023-06-12 15:51:05
這是解組目標的結構:type ParsedObjectType struct{    Value struct{        E []struct {            B bool            C float32 `json:"coefficient"`            CE float32            G int `json:"group"`            P float32 `json:"period"`            T int `json:"type"`        }    }}soucre 字符串看起來像這樣:{"B":false,"C":2.123,"CE":0,"G":1,"P":1000,"T":0}json.Unmarshal([]byte(string), ParsedObjectType)我收到后{    "B": false,    "coefficient": 0,    "CE": 0,    "group": 0,    "period": 0,    "type": 0}在屬性中使用零而不是源數據
查看完整描述

3 回答

?
炎炎設計

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

    你有兩個大問題:


您的標簽完全錯誤。例如,您的輸入包含"C":2.123,但您的結構標簽意味著 Unmarshaler 正在尋找"coefficient":2.123,它永遠找不到。要更正此問題,請設置標簽以匹配您的輸入:


type ParsedObjectType struct{

    Value struct{

        E []struct {

            B  bool

            C  float32 `json:"C"`

            CE float32

            G  int     `json:"G"`

            P  float32 `json:"P"`

            T  int     `json:"T"`

        }

    }

}

請注意,現在您的結構字段與您的 JSON 鍵完全匹配,因此如果您愿意,您可以簡單地完全消除您的 JSON 標簽以簡單起見:


type ParsedObjectType struct{

    Value struct{

        E []struct {

            B  bool

            C  float32

            CE float32

            G  int

            P  float32

            T  int

        }

    }

}

您的數據結構似乎與您的輸入不匹配。您的輸入似乎是單個對象,但您的輸入需要一個對象中的一個對象。要更正此問題(假設您在問題中提供的輸入是完整的),請刪除數據結構中的額外層:


type ParsedObjectType struct{

    B  bool

    C  float32

    CE float32

    G  int

    P  float32

    T  int

}


查看完整回答
反對 回復 2023-06-12
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

據我所知,json你有緊湊的名字,但它不應該強迫你在結構中創建神秘的名字。

在代碼中,您應該使用有意義的名稱,但在序列化格式中您可以使用同義詞。

改編自您的示例:游樂場:https://play.golang.org/p/gbWhV3FfHMr

package main


import (

    "encoding/json"

    "log"

)


type ParsedObjectType struct {

    Value struct {

        Items []struct {

            Coefficient float32 `json:"C"`

            Group       int     `json:"G"`

            Period      float32 `json:"P"`

            TypeValue   int     `json:"T"`

        } `json:"E"`

    }

}


func main() {


    str := `{"Value": {

            "E": [

              {

                "B": false,

                "C": 2.123,

                "CE": 0,

                "G": 1,

                "P": 1000,

                "T": 0

              }

            ]

          }

        }`


    out := &ParsedObjectType{}

    if err := json.Unmarshal([]byte(str), out); err != nil {

        log.Printf("failed unmarshal %s", err)

    }


    log.Printf("Constructed: %#v", out)

}


查看完整回答
反對 回復 2023-06-12
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

    如果你想解析{"B":false,"C":2.123,"CE":0,"G":1,"P":1000,"T":0}為


{

"B": false,

"coefficient": 0,

"CE": 0,

"group": 0,

"period": 0,

"type": 0

}

我認為你的結構應該聲明為


struct {

        B bool

        coefficient float32 `json:"C"`

        CE float32

        group int `json:"G"`

        period float32 `json:"P"`

        type int `json:"T"`

    }

    如果你想解析{"B":false,"C":2.123,"CE":0,"G":1,"P":1000,"T":0}為


{

"B": false,

"coefficient": 0,

"CE": 0,

"group": 0,

"period": 0,

"type": 0

}

我認為你的結構應該聲明為


struct {

        B bool

        coefficient float32 `json:"C"`

        CE float32

        group int `json:"G"`

        period float32 `json:"P"`

        type int `json:"T"`

    }


查看完整回答
反對 回復 2023-06-12
  • 3 回答
  • 0 關注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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