簡體   English   中英

嵌套Json Marshall或編碼

[英]Go Nested Json Marshall or Encoding

對於我的項目,我需要像下面這樣編碼到Json中。 我將所有值都作為變量。 任何幫助表示贊賞。

{“ id”:[{“ name”:“ Test”,“ Class”:[{“ Grade”:“ 2”,“ id”:“ 34”}]}]],“ age”:“ 5”}

這是我嘗試過的代碼

type classx  struct {
    Grade string `json:"grade"`
    Id string `json:"id"`        
}
type idx  struct {
    Name string `json:"name"`
    Class []classx


}
type Response struct {
    Age  string     `json:"age"`
    Id []idx 
} 

但是出現錯誤“無法在字段值中使用classx文字(typex類型)作為[] classx類型”

“類別”字段是一個切片。 你給了它一個結構

錯誤:

Response{Id:[]idx{idx{Class:classx{}}}}

正確:

Response{Id:[]idx{idx{Class:[]classx{}}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM