当前位置: 首页 > 面试题库 >

如何在golang中解析json数组?

叶琦
2023-03-14
问题内容
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":[]}}]

问题答案:

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

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{},但是您需要索引地图以获取不同的值,并且还需要使用类型断言来获取“类型化”值。



 类似资料:
  • 我有一个web套接字连接,它在JSON对象中发送不同类型的消息,我想将内容解组到一些已知的结构中。为此,我想我应该做以下几点: 步骤1)将JSON解组到通用映射[字符串]接口{} 第2步)找到我要找的钥匙 步骤3)尝试将值强制转换为我的类型之一(此操作失败) 步骤3(可选)json封送此值并将其解组到我的已知结构 如果我尝试使用myStruct,ok:=value。(myType)它将失败,但如果

  • 问题内容: 我正在使用一个API,该API返回如下所示的JSON 在Swift中,我使用两个函数来获取并解析JSON 然后我用 可以很好地解析JSON。当我打印出 我得到了数组的所有内容。但是,我无法访问每个单独的索引。我很肯定这是一个数组,因为我之间 返回正确的长度。但是,如果我尝试通过使用访问单个索引 XCode关闭语法高亮显示并给我以下信息: 并且代码将无法编译。 这是XCode 6的错误,

  • 问题内容: 我很难找到一种方法来解析JSONArray。看起来像这样: 如果JSON的写法不同,我知道如何解析它(换句话说,如果我返回了json对象而不是对象数组)。但这就是我所必须拥有的。 *编辑:这是一个有效的json。我使用此json制作了一个iPhone应用,现在我需要在Android上使用它,但无法弄清楚。有很多示例,但是它们都是与JSONObject相关的。我需要JSONArray。

  • 问题内容: 在python中,您可以获取json对象并从中获取特定项目,而无需声明结构,将其保存到结构中,然后像Go中那样获取值。有没有一种包装或更简单的方法来存储Go中来自json的特定值? 蟒蛇 走 问题答案: 您可以解码为,然后按键获取元素。 通常首选结构,因为它们对类型更明确。您只需要在所需的JSON中声明字段,而无需像使用映射(隐式编码/ json处理)那样键入assert值。

  • 问题内容: 我在s3中有很多gzip日志文件,其中有3种类型的日志行:b,c,i。我和c都是单层json: 类型b是深度嵌套的json。我碰到这个要点,谈论要编译一个罐子来完成这项工作。由于我的Java技能还不算出色,因此我真的不知道该怎么做。 由于类型i和c并不总是相同的顺序,因此很难在生成正则表达式中指定所有内容。Pig是否可以处理JSON(在gzip文件中)?我正在使用在Amazon Ela

  • 问题内容: 我想创建一个新闻应用程序,并且已将newsapi.org用作源。 我正在尝试获取Http库提供的JSON数据。 我在下面提供了完整的代码。 它不会给我任何错误,但不会加载任何数据,当我打印数据时,它可以打印所有内容,但无法显示。 我不是什么问题,但是我所有的项目都已停止解决此问题。 我正在寻找此代码的解决方案,因为它不起作用。 网络助手: 问题答案: 问题在于显示文章。yaa也正如@I