如何解析golang中的json数组? [英] How to parse the json array in golang?

查看:160
本文介绍了如何解析golang中的json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 包主
$ b导入(
encoding / json
fmt


类型PublicKey结构{
名称字符串
价格字符串
}

类型KeysResponse结构{
集合[] PublicKey
}
$ b $ func main(){
s:=`[{name:Galaxy Nexus,price:3460.00},{name:Galaxy Nexus价格:3460.00}]`

钥匙:= 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,键)
}
}

OutPut: [{name:price:}]



像对象数组(json数组)的复杂格式。我需要通过golang解析这个结果,我该如何实现?以下数据是我从api调用得到的结果。

  [{id:694476444991229955,id_str:694476444991229955 ,text:明白了,发件人:{id:1690262984,id_str:1690262984,name:Ashok Kumar T,screen_name:Ashok_kumar_T : 特里凡德琅, 描述: , URL:空, 实体:{ 描述:{ 网址:[]}}, 受保护:假 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:真正的 验证:假的, statuses_count:124, 郎: 恩, contributors_enabled:假的, is_translator:假的, is_translation_enabled:假的, 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:真实的, profile_i mage_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:真实的, has_extended_profile:假的, DEFAULT_PROFILE:假 default_profile_image :假的, 以下 :真实的, follow_request_sent :假的, 通知 :假}, SENDER_ID :1690262984, sender_id_str : 1690262984\" , sender_screen_name: Ashok_kumar_T, 收件人: { ID:4848598394, ID_STR: 4848598394, 名: 帕布, SCREEN_NAME: prabhu_cfml, 位置: , 说明: , URL:空, 实体:{ 描述:{ 网址:[ ]}},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:空, TIME_ZONE:空, geo_enabled:假的, 验证:假的, statuses_count:0, 郎: EN-GB, contributors_enabled:假的,is_translator :假的, is_translation_enabled :假的, profile_background_color : F5F8FA  profile_background_image_url:空, profile_background_image_url_https:空, profile_background_tile:假的, 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巴纽  profile_link_color: 2B7BB9, profile_sidebar_border_color: C0DEED, profile_sidebar_fill_color: DDEEF6, profile_text_color: 333333, profile_use_background_image:真实的, has_extended_profile:假的, DEFAULT_PROFILE :真正的 default_profile_image:T rue,following:false,follow_request_sent:false,notifications:false},recipient_id:4848598394,recipient_id_str:4848598394,recipient_screen_name:prabhu_cfml,created_at: 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 : Ashok_kumar_T, 位置: 特里凡德琅, 描述: , URL:空, 实体:{ 描述:{ 网址:[]}}, 受保护: 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 TIME_ZONE: 奈, geo_enabled:真正的 验证:假的, statuses_count:124, 郎: 恩, contributors_enabled:假的, is_translator:假的, is_translation_enabled:假, profile_background_color: 131516, profile_background_image_url:HTTP:\ / \ / abs.twimg.com\ / images\ /日emes\ / theme14\ / bg.gif, profile_background_image_url_https: HTTPS:\ / \ / abs.twimg.com\ / images\ / themes\ / theme14\ / bg.gif profile_background_tile:真实的, 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:真实的, has_extended_profile:假的DEFAULT_PROFILE:假的, default_profile_image:假的, 以下:真实的, follow_request_sent:假的, 通知:假}, SENDER_ID:1690262984, sender_id_str: 1690262984,sender_s creen_name : Ashok_kumar_T  收件人:{ ID:4848598394, ID_STR: 4848598394, 名: 帕布, SCREEN_NAME: prabhu_cfml, 位置: ,描述 :  URL:空, 实体:{ 描述:{ 网址:[]}}, 受保护:假 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, :假的, statuses_count:0, 郎: EN-GB, contributors_enabled:假的, is_translator:假的, is_translation_enabled:假的, profile_background_color: F5F8FA, profile_background_image_url:空profile_background_image_url_https:空, profile_background_tile:假的, 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_bord er_color : C0DEED, profile_sidebar_fill_color: DDEEF6, profile_text_color: 333333, profile_use_background_image:真实的, has_extended_profile:假的, DEFAULT_PROFILE:真实的, default_profile_image:真实的, 下面 :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:[]}}] 

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

 类型PublicKey结构{
名称字符串
价格字符串
}

它可以工作(在 Go Playground ):

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

请注意,JSON文本包含带有小写字母文本的字段名称,但 json 包非常聪明,足以与它们匹配。如果它们完全不同,那么可以使用struct标签告诉 json 包在JSON文本中如何找到它们(或者它们应该如何编组),例如:

 类型PublicKey结构{
名称字符串`json:some_name`
价格字符串`json: JsonPrice`
}

要解析其他JSON文本,创建一个Go模型JSON数据。我建议格式化JSON以查看真实的结构,例如您可以使用此在线JSON格式器/验证器。然后你可以解组到这个结构的一部分。



或者简单地解组成一片地图,例如 [] map [string] interface {} ,但是您需要索引映射以获取不同的值,并且还需要使用type assertion来获取typed价值。


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)
    }
}

OutPut: [{name: price:}]

I have api call returned result it is like array of object (json array) complex format. I need to parse that result by golang how can I achieve? Following data is my result it is get from api call.

[{"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":[]}}]

解决方案

You missed one point only: you need to export the fields of your struct:

type PublicKey struct {
    Name  string
    Price string
}

And it will work (try it on the Go Playground):

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

Note that the JSON text contains the field names with lowercased text, but the json package is "clever" enough to match them. If they would be completely different, you could use struct tags to tell the json package how they are found (or how they should be marshaled) in the JSON text, e.g.:

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

To parse your other JSON text, create a Go struct that models the JSON data. I suggest to format the JSON to see the real structure, e.g. you can use this online JSON formatter/validator. Then you can unmarshal into a slice of this struct.

Or simply unmarshal into a slice of maps, e.g. []map[string]interface{}, but then you need to index the map to get the different values, and you also need to use type assertion to get "typed" values.

这篇关于如何解析golang中的json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆