doujiao8491 2015-01-08 17:09
浏览 85

在Json中使用[] struct

I'm trying to parse JSON to a []struct, the JSON is retrieved from https://api.github.com/events However when I try to access each struct within the array I get an error:

type GITHUB_EVENT does not support indexing

How am I able to access each struct within the array?

func httpGetEvents() {
    eventDataRAW := httpPageGet("https://api.github.com/events", true)
    eventDataJSON := new(GITHUB_EVENT)

    _ = json.Unmarshal([]byte(eventDataRAW), &eventDataJSON)

    fmt.Println(eventDataJSON[0].Id)
}

//--------------------------------------------------------------------------------------//

type GITHUB_EVENT []struct {
    Id    string `json:"id"`
    Type  string `json:"type"`
    Actor struct {
        Id         int    `json:"id"`
        Login      string `json:"login"`
        GravatarId string `json:"gravatar_id"`
        Url        string `json:"url"`
        AvatarUrl  string `json:"avatar_url"`
    } `json:"actor"`
    Repo struct {
        Id   int    `json:"id"`
        Name string `json:"name"`
        Url  string `json:"url"`
    } `json:"repo"`
    Payload struct {
        PushId       int    `json:"push_id"`
        Size         int    `json:"size"`
        DistinctSize int    `json:"distinct_size"`
        Ref          string `json:"ref"`
        Head         string `json:"head"`
        Before       string `json:"before"`
        Commits      []struct {
            Sha    string `json:"sha"`
            Author struct {
                Email string `json:"email"`
                Name  string `json:"name"`
            } `json:"author"`
            Message  string `json:"message"`
            Distinct bool   `json:"distinct"`
            Url      string `json:"url"`
        } `json:"commits"`
    } `json:"payload"`
    Public    bool   `json:"public"`
    CreatedAt string `json:"created_at"`
}
  • 写回答

1条回答 默认 最新

  • doucheng7534 2015-01-08 17:44
    关注

    This statement:

    eventDataJSON := new(GITHUB_EVENT)
    

    declares eventDataJSON as *GITHUB_EVENT (a pointer to a slice), and initializes it as a nil pointer. After unmarshaling, you could access the first event by explicitly deref-ing the pointer before indexing:

    (*eventDataJSON)[0].Id
    

    However, the more conventional approach is to use make:

    eventDataJSON := make(GITHUB_EVENT, 0)
    

    which declares eventDataJSON as a GITHUB_EVENT, and initializes it as an empty slice. (Remember that make is for special built-in types such as slices, maps, and channels).

    You could pass a pointer to this slice to json.Unmarshal:

    _ = json.Unmarshal([]byte(eventDataRAW), &eventDataJSON)
    

    ...and then index the slice directly:

    fmt.Println(eventDataJSON[0].Id)
    

    Additionally, json.Marshal will take care of allocating the output value, so you could declare eventDataJSON and skip any explicit initialization:

    var eventDataJSON GITHUB_EVENT
    

    Example: http://play.golang.org/p/zaELDgnpB2

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题