Json 为什么结构字段显示为空?
我正在努力从以下代码中获得正确的输出:Json 为什么结构字段显示为空?,json,go,struct,Json,Go,Struct,我正在努力从以下代码中获得正确的输出: package main import ( "encoding/json" "fmt" ) func main() { var jsonBlob3 = []byte(`[ {"name": "Platypus", "spec": "Monotremata", "id":25 }, {"name": "Quoll", "spec": "Dasyuromorphia", "id":25 }
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsonBlob3 = []byte(`[
{"name": "Platypus", "spec": "Monotremata", "id":25 },
{"name": "Quoll", "spec": "Dasyuromorphia", "id":25 }
]`)
type Animal2 struct {
name string
spec string
id uint32
}
var animals []Animal2
err := json.Unmarshal(jsonBlob3, &animals)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v\n", animals)
}
游乐场
打印时,“结构”字段为空。我确信在某个地方有一个愚蠢的错误,但我仍然是新手,我已经被困在这几个小时了。请帮忙。这个问题已经提过很多次了。问题是只有导出的字段才能封送/取消封送 通过以大写字母(大写)开头导出结构字段 试穿一下 请注意,JSON文本包含带有小写文本的字段名,但是
JSON
包足够“聪明”来匹配它们。如果它们完全不同,您可以使用struct标记告诉json
包如何在json文本中找到它们(或如何封送它们),例如:
type Animal2 struct {
Name string `json:"json_name"`
Spec string `json:"specification"`
Id uint32 `json:"some_custom_id"`
}
可能重复的
type Animal2 struct {
Name string `json:"json_name"`
Spec string `json:"specification"`
Id uint32 `json:"some_custom_id"`
}