Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshal json error even a matching data structure is provided

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!

like image 346
I'm a frog dragon Avatar asked Dec 01 '25 11:12

I'm a frog dragon


1 Answers

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.

like image 142
Alexander Bauer Avatar answered Dec 03 '25 05:12

Alexander Bauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!