Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 即使提供了匹配的数据结构,也会解组json错误_Go_Json_Unmarshalling - Fatal编程技术网

Go 即使提供了匹配的数据结构,也会解组json错误

Go 即使提供了匹配的数据结构,也会解组json错误,go,json,unmarshalling,Go,Json,Unmarshalling,我从GoogleGo语言开始,试图编写一个简单的程序,从Facebook的GraphAPI获取Json对象,并将其解组 根据文档,我应该提供一个匹配的数据结构,例如,如果json对象是: { Name: "Alice" Body: "Hello", Time: 1294706395881547000, } 数据结构如下所示: type Message struct { Name string Body string Time int64 } {

我从GoogleGo语言开始,试图编写一个简单的程序,从Facebook的GraphAPI获取Json对象,并将其解组

根据文档,我应该提供一个匹配的数据结构,例如,如果json对象是:

{
    Name: "Alice"
    Body: "Hello",
    Time: 1294706395881547000,
}
数据结构如下所示:

type Message struct {
    Name string
    Body string
    Time int64
}
{
  "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"
}

在我的例子中,我的json如下所示:

type Message struct {
    Name string
    Body string
    Time int64
}
{
  "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"
}
因此,我提供了一个匹配的数据结构:

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
}

代码如下:

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
    }
}
有什么想法吗


谢谢

字段
id
未报告,因为它以小写字母开头。请参阅。

所有给定字段均为小写,因此未报告。为了解组到结构中,您需要使用

例如,在您的案例中,有效的结构是

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的行为方式,(没有链接,对不起,我有点着急),您可能完全可以不用标记。只需确保您的字段名已导出即可