douxin8610 2014-01-13 18:26
浏览 141
已采纳

无法解码JSON响应

I have the following response from a graph api

{
   "data": [
      {
         "name": "Mohamed Galib",
         "id": "502008940"
      },
      {
         "name": "Mebin Joseph",
         "id": "503453614"
      },
      {
         "name": "Rohith Raveendranath",
         "id": "507482441"
      }
   ],
   "paging": {
      "next": "https://some_url"
   }
}

I have a struct as follows

type Item struct {
   Name, Id string
}

I wanted to parse the response and get an array of Item, How do I do that?

  • 写回答

1条回答 默认 最新

  • dooso0594 2014-01-13 19:26
    关注

    You need to update your struct like so:

    type Item struct {
       Name string `json:"name"`
       Id string   `json:"id"`
    }
    

    and add a struct to represent the wrapper:

    type Data struct {
       Data []Item `json:"data"`
    }
    

    You can then use json.Unmarshal to populate a Data instance.

    See the example in the docs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记