2017-10-12 22 views
1

私は、このjsonに配列を持っています。 どのように私は得ることができます:
- full_text
- 画像リンク:mediaからとextended_entities
からmedia_url_https - URL
URL内のエンティティから - LANG
ruby​​ json配列から値を取得

[{ 
"created_at": "Thu Oct 12 14:42:03 +0000 2017", 
"id": 9184869314823331890, 
"id_str": "9184869314823331890", 
"full_text": "trying some stuff! \nhttps://somewebsite.com/emjc1MLAbD\n\uf6430\nWhat bird is this? What is its name? It has beautiful feathers. Love the mix of green, white. https://somewebsite.com/q3IqrfVcUz", 
"truncated": false, 
"display_text_range": [0, 139], 
"entities": { 
    "hashtags": [], 
    "symbols": [], 
    "user_mentions": [], 
    "urls": [{ 
     "url": "https://somewebsite.com/emjc1MLAbD", 
     "expanded_url": "https://www.pexels.com/royalty-free-images/", 
     "display_url": "pexels.com/royalty-free-i\u2026", 
     "indices": [20, 43] 
    }], 
    "media": [{ 
     "id": 918486209067827200, 
     "id_str": "918486209067827200", 
     "indices": [140, 163], 
     "media_url": "http://pbs.twimg.com/media/DL8eVz6VQAA4VHn.jpg", 
     "media_url_https": "https://pbs.twimg.com/media/DL8eVz6VQAA4VHn.jpg", 
     "url": "https://somewebsite.com/q3IqrfVcUz", 
     "display_url": "pic.twitter.com/q3IqrfVcUz", 
     "expanded_url": "https://twitter.com/DevAdama/status/918486931482333189/photo/1", 
     "type": "photo", 
     "sizes": { 
      "small": { 
       "w": 680, 
       "h": 482, 
       "resize": "fit" 
      }, 
      "medium": { 
       "w": 1200, 
       "h": 850, 
       "resize": "fit" 
      }, 
      "thumb": { 
       "w": 150, 
       "h": 150, 
       "resize": "crop" 
      }, 
      "large": { 
       "w": 1939, 
       "h": 1374, 
       "resize": "fit" 
      } 
     } 
    }] 
}, 
"extended_entities": { 
    "media": [{ 
     "id": 918486209067827200, 
     "id_str": "918486209067827200", 
     "indices": [140, 163], 
     "media_url": "http://pbs.twimg.com/media/DL8eVz6VQAA4VHn.jpg", 
     "media_url_https": "https://pbs.twimg.com/media/DL8eVz6VQAA4VHn.jpg", 
     "url": "https://somewebsite.com/q3IqrfVcUz", 
     "display_url": "pic.twitter.com/q3IqrfVcUz", 
     "expanded_url": "https://twitter.com/DevAdama/status/918486931482333189/photo/1", 
     "type": "photo", 
     "sizes": { 
      "small": { 
       "w": 680, 
       "h": 482, 
       "resize": "fit" 
      }, 
      "medium": { 
       "w": 1200, 
       "h": 850, 
       "resize": "fit" 
      }, 
      "thumb": { 
       "w": 150, 
       "h": 150, 
       "resize": "crop" 
      }, 
      "large": { 
       "w": 1939, 
       "h": 1374, 
       "resize": "fit" 
      } 
     } 
    }] 
}, 
"metadata": { 
    "iso_language_code": "en", 
    "result_type": "recent" 
}, 
"source": "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", 
"in_reply_to_status_id": null, 
"in_reply_to_status_id_str": null, 
"in_reply_to_user_id": null, 
"in_reply_to_user_id_str": null, 
"in_reply_to_screen_name": null, 
"user": { 
    "id": 915243599930982401, 
    "id_str": "915243599930982401", 
    "name": "devAdama", 
    "screen_name": "DevAdama", 
    "location": "", 
    "description": "", 
    "url": null, 
    "entities": { 
     "description": { 
      "urls": [] 
     } 
    }, 
    "protected": false, 
    "followers_count": 0, 
    "friends_count": 0, 
    "listed_count": 0, 
    "created_at": "Tue Oct 03 15:54:13 +0000 2017", 
    "favourites_count": 0, 
    "utc_offset": null, 
    "time_zone": null, 
    "geo_enabled": false, 
    "verified": false, 
    "statuses_count": 2, 
    "lang": "en", 
    "contributors_enabled": false, 
    "is_translator": false, 
    "is_translation_enabled": false, 
    "profile_background_color": "000000", 
    "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", 
    "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", 
    "profile_background_tile": false, 
    "profile_image_url": "http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png", 
    "profile_image_url_https": "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png", 
    "profile_link_color": "981CEB", 
    "profile_sidebar_border_color": "000000", 
    "profile_sidebar_fill_color": "000000", 
    "profile_text_color": "000000", 
    "profile_use_background_image": false, 
    "has_extended_profile": false, 
    "default_profile": false, 
    "default_profile_image": true, 
    "following": false, 
    "follow_request_sent": false, 
    "notifications": false, 
    "translator_type": "none" 
}, 
"geo": null, 
"coordinates": null, 
"place": null, 
"contributors": null, 
"is_quote_status": false, 
"retweet_count": 0, 
"favorite_count": 0, 
"favorited": false, 
"retweeted": false, 
"possibly_sensitive": false, 
"lang": "en" 
}, { 
"created_at": "Wed Oct 04 17:33:29 +0000 2017", 
"id": 915630969218064385, 
"id_str": "915630969218064385", 
"full_text": "hola!", 
"truncated": false, 
"display_text_range": [0, 5], 
"entities": { 
    "hashtags": [], 
    "symbols": [], 
    "user_mentions": [], 
    "urls": [] 
}, 
"metadata": { 
    "iso_language_code": "es", 
    "result_type": "recent" 
}, 
"source": "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", 
"in_reply_to_status_id": null, 
"in_reply_to_status_id_str": null, 
"in_reply_to_user_id": null, 
"in_reply_to_user_id_str": null, 
"in_reply_to_screen_name": null, 
"user": { 
    "id": 915243599930982401, 
    "id_str": "915243599930982401", 
    "name": "devAdama", 
    "screen_name": "DevAdama", 
    "location": "", 
    "description": "", 
    "url": null, 
    "entities": { 
     "description": { 
      "urls": [] 
     } 
    }, 
    "protected": false, 
    "followers_count": 0, 
    "friends_count": 0, 
    "listed_count": 0, 
    "created_at": "Tue Oct 03 15:54:13 +0000 2017", 
    "favourites_count": 0, 
    "utc_offset": null, 
    "time_zone": null, 
    "geo_enabled": false, 
    "verified": false, 
    "statuses_count": 2, 
    "lang": "en", 
    "contributors_enabled": false, 
    "is_translator": false, 
    "is_translation_enabled": false, 
    "profile_background_color": "000000", 
    "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", 
    "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", 
    "profile_background_tile": false, 
    "profile_image_url": "http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png", 
    "profile_image_url_https": "https://website.com/sticky/default_profile_images/default_profile_normal.png", 
    "profile_link_color": "981CEB", 
    "profile_sidebar_border_color": "000000", 
    "profile_sidebar_fill_color": "000000", 
    "profile_text_color": "000000", 
    "profile_use_background_image": false, 
    "has_extended_profile": false, 
    "default_profile": false, 
    "default_profile_image": true, 
    "following": false, 
    "follow_request_sent": false, 
    "notifications": false, 
    "translator_type": "none" 
}, 
"geo": null, 
"coordinates": null, 
"place": null, 
"contributors": null, 
"is_quote_status": false, 
"retweet_count": 0, 
"favorite_count": 0, 
"favorited": false, 
"retweeted": false, 
"lang": "es" 
}] 
+0

コピーエラーである可能性がありますが、 'ハッシュ'の '配列'のようです – whodini9

+0

'media'アイテムが複数ある場合どうなりますか? – whodini9

答えて

2

きちんと整えられていないかもしれませんが、引き続き次のことを試すことができます:

require 'json' 
test = JSON.parse(json) 
test.each {|k| 
p "============================" 
full_text = k['full_text'] unless k['full_text'].nil? 
urls = k['entities']['urls'] unless k['entities']['urls'].nil? 
urls.each {|u| 
    entities_url = u['url'] unless u['url'].nil? 
    p "Url: #{entities_url}" } 
lang = k['lang'] unless k['lang'] 
media = k['entities']['media'] 
extended_media = k['extended_entities']['media'] unless k['extended_entities'].nil? 
p "Full text: #{full_text}" unless full_text.nil? 

media.each {|u| 
p "Media url: #{u['media_url_https']}" unless u['media_url_https'].nil? 
} unless media.nil? 


extended_media.each {|u| 
p "Extended url: #{u['media_url_https']}" unless u['media_url_https'].nil? 
} unless extended_media.nil? 
p "Lang: #{lang}" unless lang.nil? 
p "=============================" 
} 
関連する問題