douxi3404 2018-08-11 14:15
浏览 11

在根级别解组没有键的元组

I'm trying to cut my teeth with golang, and figured parsing some json from a rest api is a good use case. It looked to be as simple as defining a struct and unmarshaling the api response into that.

Naturally the api response I am playing with is not exactly a great candidate for this. I am playing with craigslist jsonsearch - and the response is an array of 2 objects. The first object is an array of results, the second object is misc. metadata.

[  
   [  
      {  
         "Ask":6000,
         "CategoryID":145,
         "ImageThumb":"https:\/\/images.craigslist.org\/01212_dZ9PfxSmjEH_50x50c.jpg",
         "Latitude":39.591784,
         "Longitude":-105.083209,
         "PostedDate":1533949799,
         "PostingID":6642987803,
         "PostingTitle":"1991 Jeep Wrangler YJ 4.0 4X4 $6000 OBO",
         "PostingURL":"https:\/\/denver.craigslist.org\/cto\/d\/1991-jeep-wrangler-yj-40-4xobo\/6642987803.html"
      }
   ],
   {  
      "NonGeocoded":2,
      "baseurl":"\/\/denver.craigslist.org",
      "clat":41.2077284889441,
      "clng":-101.993919320865,
      "clustered":0,
      "geocoded":118,
      "zoom":7
   }
]

The fact that neither of these objects have keys is where I am getting lost.

I have created a struct that I believe should map to this response. Without keys to as field tags, I am lost... If there was only some way to say the first element in the response should map the the Results[] struct and the second item to the Metadata struct.

type SearchResponse struct {
    Results []struct {
        Ask int
        CategoryID int
        ImageThumb string
        Latitude float32
        Longitude float32
        PostedDate int64
        PostingID int64
        PostingTitle string
        PostingURL string
    }`json:"??first element??"`
    Metadata struct{
        NonGeocoded int
        baseurl string
        clat float32
        clng float32
        clustered int
        geocoded int
        zoom int
    }`json:"??second element??"`
}


func main() {

    searchUrl := "https://denver.craigslist.org/jsonsearch/cta?query=jeep+wrangler&sort=rel&max_price=15000&auto_transmission=1"
    resp, _ := http.Get(searchUrl)
    bytes, _ := ioutil.ReadAll(resp.Body)

    var searchResp SearchResponse
    if err := json.Unmarshal(bytes, &searchResp); err != nil {
        panic(err)
    }

    fmt.Print("it worked!")
    resp.Body.Close()

}

Is there a easier/better way to do this?

  • 写回答

1条回答 默认 最新

  • dtwxt88240 2018-08-24 01:50
    关注

    Just use this one https://github.com/Anderson-Lu/gofasion. It's easy.

    Gofasion is a lightweight parsing library that facilitates the parsing of interface JSON data during development. Its biggest feature is to support chained calls, which means that the target key name and key value can be directly obtained without pre-defining the structure of the data.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b