Unmarshal json error even a matching data structure is provided

自作多情 提交于 2019-12-24 03:36:19

问题


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:


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




回答2:


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.



来源:https://stackoverflow.com/questions/14441149/unmarshal-json-error-even-a-matching-data-structure-is-provided

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!