dongyi1921 2016-02-04 13:57
浏览 171
已采纳

如何在golang中解析json数组?

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
", keys)
    } else {
        fmt.Println(err)
        fmt.Printf("%+v
", 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":[]}}]
  • 写回答

1条回答 默认 最新

  • douyong5476 2016-02-04 14:04
    关注

    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.

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

报告相同问题?

悬赏问题

  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信