Golang – 无法解析 JSON

huangapple go评论72阅读模式
英文:

Golang - unable to parse JSON

问题

我开始学习GO,并遇到了这个问题。
我有以下代码:

package main

import (
  "log"
  "encoding/json"
)


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 {
    log.Fatalf("%s : 解析json时出错", err)
  }
  log.Printf("%+v\n",w)
}

当我运行它时,显示如下:

[nap@rhel projects]$ go run euler.go 
{name: cod:0 dt:0 id:0}

所以,JSON没有解析到weather结构体中。

有任何想法,为什么会出现这种情况?

英文:

I've started learning GO, and i have encountered this issue.
I have this code

package main

import (
  "log"
  "encoding/json"
)


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 {
    log.Fatalf("%s : while parsing json", err)
  }
  log.Printf("%+v\n",w)
}

when i run it, it shows this

[nap@rhel projects]$ go run euler.go 
{name: cod:0 dt:0 id:0}

So, the JSON is not parsed into the weather struct.

Any ideas, why do this happens like this?

答案1

得分: 7

你需要将结构字段的首字母大写。例如:

Name string
Cod  float64
// 等等...

这是因为在尝试解组时,这些字段对于json包是不可见的。

Playground链接:http://play.golang.org/p/cOJD4itfIS

英文:

You need to capitalise your struct fields. E.g:

Name string
Cod  float64
// etc..

This is because they are not visible to the json package when attempting to unmarshal.

Playground link: http://play.golang.org/p/cOJD4itfIS

答案2

得分: 1

这是因为Weather没有导出任何字段。这样修改:

type Weather struct {
   Name string
   Cod float64
   Dt float64
   Id float64
}

如果一个字段没有被导出,json包将无法访问它。你可以在这里找到更多信息:http://blog.golang.org/json-and-go,简而言之:

“json包只能访问结构体类型的导出字段(以大写字母开头的字段)。因此,只有结构体的导出字段会出现在JSON输出中。”

英文:

This is because Weather does not export any fields. This;

type Weather struct {
   name string
   cod float64
   dt float64
   id float64
}

Needs to be;

type Weather struct {
   Name string
   Cod float64
   Dt float64
   Id float64
}

If a field is not exported then the json package will not be able to access it. You can find more information about it here http://blog.golang.org/json-and-go but the short version is;

" The json package only accesses the exported fields of struct types (those that begin with an uppercase letter). Therefore only the the exported fields of a struct will be present in the JSON output."

答案3

得分: 0

通过猜测,我发现这段代码是有效的:

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)
}

所以,结构体字段的名称必须大写才能正常工作!

英文:

By quessing, i found, that this code works:

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)
}

So, the struct field names have to be Capitalized to work properly!

huangapple
  • 本文由 发表于 2014年8月4日 06:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/25109880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定