doujiu3097 2016-02-05 05:04
浏览 34
已采纳

too long

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

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

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

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
", keys)
    } else {
        fmt.Println(err)
        fmt.Printf("%+v
", keys)
    }
    myId := keys[l].Id
    fmt.Printf(myId)
}

</div>
  • 写回答

1条回答 默认 最新

  • duangan2307 2016-02-05 05:51
    关注

    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

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?