如何将带有小写成员的简单json分解为一个结构



我有一个带有字符串和int的简单结构。当我解组json时,如果结构成员以小写字符串开头,则不会对其进行解析。即使我在同一个包中使用

package main
import (
"encoding/json"
"fmt"
)
type Bird struct {
Species string
Description string
lifespan int
}


func main() {
birdJson := `{"species": "pigeon","description": "likes to perch on rocks","lifespan": 9}`
var bird Bird   
json.Unmarshal([]byte(birdJson), &bird)
fmt.Printf("Species: %s, Description: %s,lifespan: %d", bird.Species, bird.Description,bird.lifespan)
//Cant read the lifespan ??
}
lifespan int

需要

Lifespan int

你不能将其分解为未过期的字段

最新更新