2016-02-05 67 views
0

我想從twitter使用twitter API獲取直接消息。我得到了API JSON數組respose像如何使用golang從解析的json數組中獲取特定值?

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

我已經成功地解析了json.But我無法得到解析的JSON的具體數據。在這裏我的打法golang鏈接http://play.golang.org/p/zS42Qws2Di

package main 
 

 
import (
 
\t "encoding/json" 
 
\t "fmt" 
 
\t 
 
\t 
 
) 
 

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

 
type KeysResponse struct { 
 
\t Collection []PublicKey 
 
} 
 

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

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

回答

1

如果目標是打印的第二個數組元素的標識,然後使用此代碼:

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

playground example

相關問題