解析JSON错误,即使提供了匹配的数据结构。

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

Unmarshal json error even a matching data structure is provided

问题

type Graph struct {
id string
name string
description string
category string
subcategory string
link string
namespace string
icon_url string
logo_url string
company string
}

func main() {
resp, err := http.Get("http://graph.facebook.com/android")

if err != nil {
    fmt.Println("http.Get err: ",err)// handle error
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)    

var g Graph
err = json.Unmarshal(body, &g)
if err == nil {
    fmt.Println("graph: ", g)
} else {
    fmt.Println("graph error: ",err) // <---error at this line
}

}

英文:

I'm starting out with Google go language, and I'm trying to write a simple program to obtain a Json object from Facebook's graph API and unmarshal it.

According to the documentation, http://golang.org/doc/articles/json_and_go.html, I should provide a matching data structure, for example if the json object is:

{
    Name: &quot;Alice&quot;
    Body: &quot;Hello&quot;,
    Time: 1294706395881547000,
}

The data structure will look like this:

type Message struct {
    Name string
    Body string
    Time int64
}

In my case, my json look like this:

{
  &quot;id&quot;: &quot;350685531728&quot;,
   &quot;name&quot;: &quot;Facebook for Android&quot;,
   &quot;description&quot;: &quot;Keep up with friends, wherever you are.&quot;,
   &quot;category&quot;: &quot;Utilities&quot;,
   &quot;subcategory&quot;: &quot;Communication&quot;,
   &quot;link&quot;: &quot;http://www.facebook.com/apps/application.php?id=350685531728&quot;,
   &quot;namespace&quot;: &quot;fbandroid&quot;,
   &quot;icon_url&quot;: &quot;http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/OKB7Z2hkREe.png&quot;,
   &quot;logo_url&quot;: &quot;http://photos-b.ak.fbcdn.net/photos-ak-snc7/v43/207/227200684078827/app_115_227200684078827_474764570.png&quot;,
   &quot;company&quot;: &quot;Facebook&quot;
}

So I provided a matching data structure:

type Graph struct {
    id string
    name string
    description string
    category string
    subcategory string
    link string
    namespace string
    icon_url string
    logo_url string
    company string
}

This is the code:

package main

import &quot;fmt&quot;
import &quot;net/http&quot;
import &quot;io/ioutil&quot;
import &quot;encoding/json&quot;

type Graph struct {
    id string
    name string
    description string
	category string
	subcategory string
	link string
	namespace string
	icon_url string
	logo_url string
	company string
}

func main() {
    fmt.Println(&quot;Hello, playground&quot;)
    resp, err := http.Get(&quot;http://graph.facebook.com/android&quot;)

	if err != nil {
		fmt.Println(&quot;http.Get err: &quot;,err)// handle error
	}
	
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)	
	
	fmt.Println(&quot;resp.Body: &quot;, resp.Body)	
	fmt.Println(&quot;body: &quot;,string(body))
	fmt.Println(&quot;err: &quot;,err)

	var g Graph
    err = json.Unmarshal(body, &amp;g)
	if err == nil {
		fmt.Println(&quot;graph: &quot;, g)
	} else {
		fmt.Println(&quot;graph error: &quot;,err) // &lt;---error at this line
	}
}

But I'm still getting the following error:

graph error:  json: cannot unmarshal object key &quot;id&quot; into unexported field id of type main.Graph

Any idea?

Thanks!

答案1

得分: 12

所有给定的字段都是小写的,因此未导出。为了将其解组成结构体,您需要使用标签

例如,在您的情况下,一个有效的结构体应该是:

type Graph struct {
    Id          string `json:"id"`
    Name        string `json:"name"`
    Description string `json:"description"`
    Category    string `json:"category"`
    Subcategory string `json:"subcategory"`
    Link        string `json:"link"`
    Namespace   string `json:"namespace"`
    Icon_url    string `json:"icon_url"`
    Logo_url    string `json:"logo_url"`
    Company     string `json:"company"`
}

然而,由于encoding/json的行为方式(没有链接,抱歉,我有点匆忙),您可能可以完全不使用标签。只需确保您的字段名已导出。

英文:

All of the given fields are lowercase, and therefore unexported. In order to unmarshal into the structure, you'll need to make use of tags.

For example, a valid structure in your case would be

type Graph struct {
    Id          string `json:&quot;id&quot;`
    Name        string `json:&quot;name&quot;`
    Description string `json:&quot;description&quot;`
    Category    string `json:&quot;category&quot;`
    Subcategory string `json:&quot;subcategory&quot;`
    Link        string `json:&quot;link&quot;`
    Namespace   string `json:&quot;namespace&quot;`
    Icon_url    string `json:&quot;icon_url&quot;`
    Logo_url    string `json:&quot;logo_url&quot;`
    Company     string `json:&quot;company&quot;`
}

However, because of the way that encoding/json behaves, (no link, sorry, I'm in a bit of a rush,) you may be able to get by without the tags entirely. Just make sure that your field names are exported.

答案2

得分: 8

字段id是未导出的,因为它以小写字母开头。请参阅导出标识符

英文:

The field id is unexported because it starts with a lower case letter. See Exported identifiers.

huangapple
  • 本文由 发表于 2013年1月21日 22:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/14441149.html
匿名

发表评论

匿名网友

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

确定