转到:解组JSON嵌套的对象数组

nickcoxdotme:

我知道您可以将任意JSON解组为一个map[string]interface{},而在这种情况下,我的JSON响应始终是定义且一致的,为了简单起见,我宁愿将其解组为嵌套结构。

这是缩写为JSON的示例:

{
  (...)
  "results": [
    {
      "section": "N.Y. / Region",
      "subsection": "",
      "title": "Christie Spins His Version of Security Record on Trail",
      "abstract": "An examination of Gov. Chris Christie’s record as New Jersey’s top federal prosecutor shows that he has, at times, overstated the significance of the terrorism prosecutions he oversaw.",
      "url": "http://www.nytimes.com/2015/12/27/nyregion/Christie-markets-himself-as-protector-to-gain-in-polls.html",
      "byline": "By ALEXANDER BURNS and CHARLIE SAVAGE",
      "item_type": "Article",
      "updated_date": "2015-12-26T18:04:19-5:00",
      (...)
      "multimedia": [
        {
          "url": "http://static01.nyt.com/images/2015/12/27/nyregion/27CHRISTIE1/27CHRISTIE1-thumbStandard.jpg",
          "format": "Standard Thumbnail",
          "height": 75,
          "width": 75,
          "type": "image",
          "subtype": "photo",
          "caption": "Gov. Chris Christie of New Jersey spoke about the Sept. 11, 2001, attacks at a Republican conference last month.",
          "copyright": "Stephen Crowley/The New York Times"
        }
        (...)
      ]
    }
  ]
}

我尝试过的

我尝试使用JSONutils自动执行结构创建,并最终完成了此操作(删除了我不想要的字段后):

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type PoliticsJson struct {
    Results []struct {
        Multimedia []struct {
            URL string `json:"url"`
        } `json:"multimedia"`
        Title string `json:"title"`
    } `json:"results"`
}

func retrieveData() []byte {
    url := "http://api.nytimes.com/svc/topstories/v1/politics.json?MYAPIKEY"
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err2 := ioutil.ReadAll(resp.Body)
    if err2 != nil {
        fmt.Println(err2)
    }
    return body
}


func main() {
    var p PoliticsJson

    err := json.Unmarshal(retrieveData(), &p)
    if err != nil {
        panic(err)
    }
    fmt.Println(p.Results[0].Title)
}

我基本上只想打印出多媒体数组中最后一个对象的标题和URL。(我只是想使其正常工作,所以请原谅错误处理。)

这是我得到的错误: panic: json: cannot unmarshal string into Go value of type []struct { URL string "json:\"url\"" }

问题Multimedia显然在于结构。令我感到困惑的是,该错误似乎表明它正在被解释为字符串,但是我将结构更改为以确保:

type PoliticsJson struct {
    Results []struct {
        Multimedia string `json:"multimedia"`
        Title      string `json:"title"`
    } `json:"results"`
}

而且我得到了panic: json: cannot unmarshal array into Go value of type string,这表明它被解释为JSON的数组。

同样,我基本上只想打印出多媒体数组中最后一个对象的标题和URL。

Prashant Thakkar:

使用您的PoliticsJson和Sample Json字符串进行了尝试,能够解析以获得标题和URL。

我尝试通过将URL更改为static02来为多媒体添加一个条目,并且能够同时打印两个URL。

以下是指向PlayGround的链接:http : //play.golang.org/p/rAJfkD1i7n

编辑

发现问题的原因是,在结果的2个中,多媒体是字符串,即"multimedia":""检查行975和1319。由于期望数组,所以json失败,因为找到了字符串,将其转换为"multimedia":[]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章