Golang - 无法解析JSON

vod*_*095 2 json go unmarshalling

我开始学习GO了,我遇到过这个问题.我有这个代码

package main

import (
  "fmt"
  "encoding/json"
//  "net/html"
)


func main(){
  type Weather struct {
    name string
    cod float64
    dt float64
    id float64
  }

  var rawWeather = []byte(`{"name":"London","cod":200,"dt":1407100800,"id":2643743}`)
  var w Weather
  err := json.Unmarshal(rawWeather, &w)
  if err != nil {
    fmt.Println("Some error", err)
  }
  fmt.Printf("%+v\n",w)
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它显示了这一点

[nap@rhel projects]$ go run euler.go 
{name: cod:0 dt:0 id:0}
Run Code Online (Sandbox Code Playgroud)

因此,JSON不会被解析为weather结构.

任何想法,为什么会这样发生?

Sim*_*ead 6

您需要将结构字段大写.例如:

Name string
Cod  float64
// etc..
Run Code Online (Sandbox Code Playgroud)

这是因为json在尝试解组时,包不可见.

游乐场链接:http://play.golang.org/p/cOJD4itfIS