如何在golang中解析json数组?

cfprabhu:
package main

import (
    "encoding/json"
    "fmt"
)

type PublicKey struct {
    name string
    price string
}

type KeysResponse struct {
    Collection []PublicKey
}

func main() {
    s := `[{"name":"Galaxy Nexus", "price":"3460.00"},{"name":"Galaxy Nexus", "price":"3460.00"}]`

    keys := make([]PublicKey,0)
    err := json.Unmarshal([]byte(s), &keys)
    if err == nil {
        fmt.Printf("%+v\n", keys)
    } else {
        fmt.Println(err)
        fmt.Printf("%+v\n", keys)
    }
}

输出: [{name: price:}]

我有api调用返回的结果,它就像对象数组(json数组)的复杂格式。我需要通过golang解析该结果,我该如何实现?以下数据是我的结果,它是从api调用获取的。

[{"id":694476444991229955,"id_str":"694476444991229955","text":"Got it","sender":{"id":1690262984,"id_str":"1690262984","name":"Ashok Kumar T","screen_name":"Ashok_kumar_T","location":"Trivandrum","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":68,"friends_count":32,"listed_count":0,"created_at":"Thu Aug 22 06:52:53 +0000 2013","favourites_count":5,"utc_offset":19800,"time_zone":"Chennai","geo_enabled":true,"verified":false,"statuses_count":124,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252","profile_link_color":"009999","profile_sidebar_border_color":"EEEEEE","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false},"sender_id":1690262984,"sender_id_str":"1690262984","sender_screen_name":"Ashok_kumar_T","recipient":{"id":4848598394,"id_str":"4848598394","name":"Prabhu","screen_name":"prabhu_cfml","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":1,"friends_count":1,"listed_count":0,"created_at":"Tue Feb 02 09:25:17 +0000 2016","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":0,"lang":"en-gb","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_3_normal.png","profile_image_url_https":"https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_3_normal.png","profile_link_color":"2B7BB9","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false},"recipient_id":4848598394,"recipient_id_str":"4848598394","recipient_screen_name":"prabhu_cfml","created_at":"Tue Feb 02 11:04:21 +0000 2016","entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]}},{"id":694476425793916931,"id_str":"694476425793916931","text":"Hi","sender":{"id":1690262984,"id_str":"1690262984","name":"Ashok Kumar T","screen_name":"Ashok_kumar_T","location":"Trivandrum","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":68,"friends_count":32,"listed_count":0,"created_at":"Thu Aug 22 06:52:53 +0000 2013","favourites_count":5,"utc_offset":19800,"time_zone":"Chennai","geo_enabled":true,"verified":false,"statuses_count":124,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252","profile_link_color":"009999","profile_sidebar_border_color":"EEEEEE","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false},"sender_id":1690262984,"sender_id_str":"1690262984","sender_screen_name":"Ashok_kumar_T","recipient":{"id":4848598394,"id_str":"4848598394","name":"Prabhu","screen_name":"prabhu_cfml","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":1,"friends_count":1,"listed_count":0,"created_at":"Tue Feb 02 09:25:17 +0000 2016","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":0,"lang":"en-gb","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_3_normal.png","profile_image_url_https":"https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_3_normal.png","profile_link_color":"2B7BB9","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false},"recipient_id":4848598394,"recipient_id_str":"4848598394","recipient_screen_name":"prabhu_cfml","created_at":"Tue Feb 02 11:04:17 +0000 2016","entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]}}]
icza:

您仅错过了一点:您需要导出结构的字段:

type PublicKey struct {
    Name  string
    Price string
}

它将起作用(在Go Playground上尝试):

[{Name:Galaxy Nexus Price:3460.00} {Name:Galaxy Nexus Price:3460.00}]

请注意,JSON文本包含带有小写字母文本的字段名称,但是该json包“足够聪明”以匹配它们。如果它们完全不同,则可以使用struct标记来告诉json程序包如何在JSON文本中找到它们(或应如何封送它们),例如:

type PublicKey struct {
    Name  string `json:"some_name"`
    Price string `json:"JsonPrice"`
}

要解析其他JSON文本,请创建一个对JSON数据建模的Go结构。我建议对JSON进行格式化以查看其真实结构,例如,您可以使用此在线JSON格式化程序/验证器然后,您可以解编为该结构的一部分。

或简单地将其编组为地图的一部分,例如[]map[string]interface{},但是您需要索引地图以获取不同的值,并且还需要使用类型断言来获取“类型化”值。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在GOLANG中解析JSON哈希的JSON数组

来自分类Dev

如何在golang中解析json数组结构

来自分类Dev

如何在golang中解析JSON?

来自分类Dev

如何在Golang中解析巨大的json

来自分类Dev

如何在Golang中的嵌套JSON对象中解析嵌套数组?

来自分类Dev

如何在Golang中遍历JSON数组?

来自分类Dev

如何在 postgresql 中解析 JSON 数组?

来自分类Dev

如何在Go中解析数组json?

来自分类Dev

如何在 Javascript 中解析 Json 数组

来自分类Dev

在Golang中解析JSON数组

来自分类Dev

如何在侧面JSON数组Android中解析JSON数组

来自分类Java

如何在Android中解析JSON数组(非Json对象)

来自分类Dev

golang,如何在simplejson中解码json数组的项目?

来自分类Dev

使用golang中的数组解析JSON

来自分类Dev

无法在Golang中解析JSON数组

来自分类Dev

如何在Postgres查询中解析JSON数组

来自分类Dev

如何在vb.net中解析json数组?

来自分类Dev

如何在PostgreSQL函数中优化解析Json数组

来自分类Dev

如何在st中解析来自stdin的无限json数组?

来自分类Dev

如何在Qt中解析多维JSON数组?

来自分类Dev

如何在C#中解析JSON数组?

来自分类Dev

如何在节点js中解析JSON数组

来自分类Dev

如何在Volley中解析多维json数组?

来自分类Dev

如何在 Swift 5 中解析字典和数组 JSON

来自分类Dev

如何在jQuery中解析JSON对象数组

来自分类Dev

如何在Swift中解析具有对象的JSON数组

来自分类Dev

如何在Flutter中解析此JSON数组?

来自分类Dev

如何在scala play框架中解析json数组?

来自分类Dev

如何在Flutter中解析长度为1的JSON数组?