-3

I have a simple struct with string and int. When I unmarshal a json the struct members are not getting parsed if beginning with lowercase string .Even if I am using in the same package

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 ??
}
Ram
  • 1,155
  • 13
  • 34

1 Answers1

0
lifespan int

needs to be

Lifespan int

you can't unmarshal into an unexported field

dave
  • 62,300
  • 5
  • 72
  • 93