英文:
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: "Alice"
Body: "Hello",
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:
{
"id": "350685531728",
"name": "Facebook for Android",
"description": "Keep up with friends, wherever you are.",
"category": "Utilities",
"subcategory": "Communication",
"link": "http://www.facebook.com/apps/application.php?id=350685531728",
"namespace": "fbandroid",
"icon_url": "http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/OKB7Z2hkREe.png",
"logo_url": "http://photos-b.ak.fbcdn.net/photos-ak-snc7/v43/207/227200684078827/app_115_227200684078827_474764570.png",
"company": "Facebook"
}
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 "fmt"
import "net/http"
import "io/ioutil"
import "encoding/json"
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("Hello, playground")
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)
fmt.Println("resp.Body: ", resp.Body)
fmt.Println("body: ",string(body))
fmt.Println("err: ",err)
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
}
}
But I'm still getting the following error:
graph error: json: cannot unmarshal object key "id" 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:"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"`
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论