如何使用Golang从解析的JSON数组中获取特定值?

huangapple go评论88阅读模式
英文:

How to get the particular value from parsed json array by using golang?

问题

我正在尝试使用Twitter API获取Twitter的直接消息。我得到了API的JSON数组响应,如下所示:

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

我已成功解析了JSON,但无法从解析后的JSON中获取特定的数据。这是我的Play Golang链接:http://play.golang.org/p/zS42Qws2Di

package main

import (
	"encoding/json"
	"fmt"
)

type PublicKey struct {
	ID              int64
	ID_STR          string
	Text            string
	SENDER          struct {
		ID      int64
		ID_STR  string
		NAME    string
	}
	PROTECTED       bool
	FOLLOWERS_COUNT int
	FRIENDS_COUNT   int
	LISTED_COUNT    int
}

type KeysResponse struct {
	Collection []PublicKey
}

func main() {
	s := `[{
		"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
		},
		"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
	},{
		"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
		},
		"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
	}]`

	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)
	}
	myId := keys[l].ID
	fmt.Printf(myId)
}

请注意,这只是一个示例代码,其中的变量l没有定义。你需要根据你的需求修改代码以获取特定的数据。

英文:

I am trying to get direct message from twitter using twitter api. I got the api json array respose like

<!-- begin snippet: js hide: false -->

<!-- language: lang-js -->

[{
&quot;id&quot;: 694476444991229955,
&quot;id_str&quot;: &quot;694476444991229955&quot;,
&quot;text&quot;: &quot;Got it&quot;,
&quot;sender&quot;: {
&quot;id&quot;: 1690262984,
&quot;id_str&quot;: &quot;1690262984&quot;,
&quot;name&quot;: &quot;Ashok Kumar T&quot;,
&quot;screen_name&quot;: &quot;Ashok_kumar_T&quot;,
&quot;location&quot;: &quot;Trivandrum&quot;,
&quot;description&quot;: &quot;&quot;,
&quot;url&quot;: null
},
&quot;protected&quot;: false,
&quot;followers_count&quot;: 68,
&quot;friends_count&quot;: 32,
&quot;listed_count&quot;: 0,
&quot;created_at&quot;: &quot;Thu Aug 22 06:52:53 +0000 2013&quot;,
&quot;favourites_count&quot;: 5,
&quot;utc_offset&quot;: 19800,
&quot;time_zone&quot;: &quot;Chennai&quot;,
&quot;geo_enabled&quot;: true,
&quot;verified&quot;: false,
&quot;statuses_count&quot;: 124,
&quot;lang&quot;: &quot;en&quot;,
&quot;contributors_enabled&quot;: false,
&quot;is_translator&quot;: false,
&quot;is_translation_enabled&quot;: false,
&quot;profile_background_color&quot;: &quot;131516&quot;,
&quot;profile_background_image_url&quot;: &quot;http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif&quot;,
&quot;profile_background_image_url_https&quot;: &quot;https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif&quot;,
&quot;profile_background_tile&quot;: true,
&quot;profile_image_url&quot;: &quot;http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg&quot;,
&quot;profile_image_url_https&quot;: &quot;https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg&quot;,
&quot;profile_banner_url&quot;: &quot;https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252&quot;,
&quot;profile_link_color&quot;: &quot;009999&quot;,
&quot;profile_sidebar_border_color&quot;: &quot;EEEEEE&quot;,
&quot;profile_sidebar_fill_color&quot;: &quot;EFEFEF&quot;,
&quot;profile_text_color&quot;: &quot;333333&quot;,
&quot;profile_use_background_image&quot;: true,
&quot;has_extended_profile&quot;: false,
&quot;default_profile&quot;: false,
&quot;default_profile_image&quot;: false,
&quot;following&quot;: true,
&quot;follow_request_sent&quot;: false,
&quot;notifications&quot;: false
}]

<!-- end snippet -->

I have successfully parsed the json.But I cant get the particular data from parsed json. Here my play golang link http://play.golang.org/p/zS42Qws2Di

<!-- begin snippet: js hide: false -->

<!-- language: lang-js -->

package main
import (
&quot;encoding/json&quot;
&quot;fmt&quot;
)
type PublicKey struct {
ID int64
ID_STR string
Text string
SENDER struct {
ID int64
ID_STR string
NAME string
}
PROTECTED bool
FOLLOWERS_COUNT int
FRIENDS_COUNT int
LISTED_COUNT int
}
type KeysResponse struct {
Collection []PublicKey
}
func main() {
s := `[{
&quot;id&quot;: 694476444991229955,
&quot;id_str&quot;: &quot;694476444991229955&quot;,
&quot;text&quot;: &quot;Got it&quot;,
&quot;sender&quot;: {
&quot;id&quot;: 1690262984,
&quot;id_str&quot;: &quot;1690262984&quot;,
&quot;name&quot;: &quot;Ashok Kumar T&quot;,
&quot;screen_name&quot;: &quot;Ashok_kumar_T&quot;,
&quot;location&quot;: &quot;Trivandrum&quot;,
&quot;description&quot;: &quot;&quot;,
&quot;url&quot;: null
},
&quot;protected&quot;: false,
&quot;followers_count&quot;: 68,
&quot;friends_count&quot;: 32,
&quot;listed_count&quot;: 0,
&quot;created_at&quot;: &quot;Thu Aug 22 06:52:53 +0000 2013&quot;,
&quot;favourites_count&quot;: 5,
&quot;utc_offset&quot;: 19800,
&quot;time_zone&quot;: &quot;Chennai&quot;,
&quot;geo_enabled&quot;: true,
&quot;verified&quot;: false,
&quot;statuses_count&quot;: 124,
&quot;lang&quot;: &quot;en&quot;,
&quot;contributors_enabled&quot;: false,
&quot;is_translator&quot;: false,
&quot;is_translation_enabled&quot;: false,
&quot;profile_background_color&quot;: &quot;131516&quot;,
&quot;profile_background_image_url&quot;: &quot;http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif&quot;,
&quot;profile_background_image_url_https&quot;: &quot;https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif&quot;,
&quot;profile_background_tile&quot;: true,
&quot;profile_image_url&quot;: &quot;http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg&quot;,
&quot;profile_image_url_https&quot;: &quot;https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg&quot;,
&quot;profile_banner_url&quot;: &quot;https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252&quot;,
&quot;profile_link_color&quot;: &quot;009999&quot;,
&quot;profile_sidebar_border_color&quot;: &quot;EEEEEE&quot;,
&quot;profile_sidebar_fill_color&quot;: &quot;EFEFEF&quot;,
&quot;profile_text_color&quot;: &quot;333333&quot;,
&quot;profile_use_background_image&quot;: true,
&quot;has_extended_profile&quot;: false,
&quot;default_profile&quot;: false,
&quot;default_profile_image&quot;: false,
&quot;following&quot;: true,
&quot;follow_request_sent&quot;: false,
&quot;notifications&quot;: false
},{
&quot;id&quot;: 694476444991229955,
&quot;id_str&quot;: &quot;694476444991229955&quot;,
&quot;text&quot;: &quot;Got it&quot;,
&quot;sender&quot;: {
&quot;id&quot;: 1690262984,
&quot;id_str&quot;: &quot;1690262984&quot;,
&quot;name&quot;: &quot;Ashok Kumar T&quot;,
&quot;screen_name&quot;: &quot;Ashok_kumar_T&quot;,
&quot;location&quot;: &quot;Trivandrum&quot;,
&quot;description&quot;: &quot;&quot;,
&quot;url&quot;: null
},
&quot;protected&quot;: false,
&quot;followers_count&quot;: 68,
&quot;friends_count&quot;: 32,
&quot;listed_count&quot;: 0,
&quot;created_at&quot;: &quot;Thu Aug 22 06:52:53 +0000 2013&quot;,
&quot;favourites_count&quot;: 5,
&quot;utc_offset&quot;: 19800,
&quot;time_zone&quot;: &quot;Chennai&quot;,
&quot;geo_enabled&quot;: true,
&quot;verified&quot;: false,
&quot;statuses_count&quot;: 124,
&quot;lang&quot;: &quot;en&quot;,
&quot;contributors_enabled&quot;: false,
&quot;is_translator&quot;: false,
&quot;is_translation_enabled&quot;: false,
&quot;profile_background_color&quot;: &quot;131516&quot;,
&quot;profile_background_image_url&quot;: &quot;http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif&quot;,
&quot;profile_background_image_url_https&quot;: &quot;https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif&quot;,
&quot;profile_background_tile&quot;: true,
&quot;profile_image_url&quot;: &quot;http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg&quot;,
&quot;profile_image_url_https&quot;: &quot;https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg&quot;,
&quot;profile_banner_url&quot;: &quot;https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252&quot;,
&quot;profile_link_color&quot;: &quot;009999&quot;,
&quot;profile_sidebar_border_color&quot;: &quot;EEEEEE&quot;,
&quot;profile_sidebar_fill_color&quot;: &quot;EFEFEF&quot;,
&quot;profile_text_color&quot;: &quot;333333&quot;,
&quot;profile_use_background_image&quot;: true,
&quot;has_extended_profile&quot;: false,
&quot;default_profile&quot;: false,
&quot;default_profile_image&quot;: false,
&quot;following&quot;: true,
&quot;follow_request_sent&quot;: false,
&quot;notifications&quot;: false
}]`
keys := make([]PublicKey,0)
err := json.Unmarshal([]byte(s), &amp;keys)
if err == nil {
fmt.Printf(&quot;%+v\n&quot;, keys)
} else {
fmt.Println(err)
fmt.Printf(&quot;%+v\n&quot;, keys)
}
myId := keys[l].Id
fmt.Printf(myId)
}

<!-- end snippet -->

答案1

得分: 1

如果目标是打印第二个数组元素的标识符,请使用以下代码:

myId := keys[1].ID  // 将1改为l,将Id改为ID
fmt.Println(myId)

playground示例

英文:

If the goal is to print the identifier of the second array element, then use this code:

myId := keys[1].ID  // l changed 1, Id changed to ID
fmt.Println(myId)

playground example

huangapple
  • 本文由 发表于 2016年2月5日 13:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/35216612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定