java - Unable to Retrieve Place Coordinates from Twitter Geocode -
i running issue logic unable retrieve first 2 coordinates in json array twitter feed. using jackson jsonnode retrieve twitter feed.
below logic have first validating node not null , if not null attempt retrieve first 2 lon/lat coordinates plane.
problem never able retrieve coordinates retweet -> places -> bounding_box -> coordinates -> 0 0 & 0 1
source code logic
if(root.path("retweeted_status").path("place").path("bounding_box").get("coordinates") != null){ lon = root.path("retweeted_status").path("place").path("bounding_box").path("coordinates").get(0).get(0).textvalue(); lat = root.path("retweeted_status").path("place").path("bounding_box").path("coordinates").get(0).get(1).textvalue(); if(root.path("retweeted_status").path("place").get("full_name") != null) { userlocation = root.path("retweeted_status").path("place").get("full_name").textvalue(); } }
section trying retrieve, first 2 geo codes in bounding_box array
"place": { "id": "5a110d312052166f", "url": "https:\/\/api.twitter.com\/1.1\/geo\/id\/5a110d312052166f.json", "place_type": "city", "name": "san francisco", "full_name": "san francisco, ca", "country_code": "us", "country": "united states", "bounding_box": { "type": "polygon", "coordinates": [ [ [-122.514926, 37.708075], [-122.514926, 37.833238], [-122.357031, 37.833238], [-122.357031, 37.708075] ] ] }, "attributes": {} },
sample json
{ "created_at":"fri feb 05 01:36:27 +0000 2016", "id":695420692209111000, "id_str":"695420692209111040", "text":"rt @sayhop: when boyfriend doesn't watch show #realfansgetit x1 @xfinity #ad https://t.co/e4h3zo92nv", "source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">twitter iphone</a>", "truncated":false, "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":278162142, "id_str":"278162142", "name":"alexeia ♡", "screen_name":"_vievelv_", "location":null, "url":null, "description":"itty bitty ❤️", "protected":false, "verified":false, "followers_count":1571, "friends_count":1246, "listed_count":0, "favourites_count":344, "statuses_count":21194, "created_at":"wed apr 06 18:46:36 +0000 2011", "utc_offset":-18000, "time_zone":"eastern time (us & canada)", "geo_enabled":true, "lang":"en", "contributors_enabled":false, "is_translator":false, "profile_background_color":"642d8b", "profile_background_image_url":"http://pbs.twimg.com/profile_background_images/656263432/w1zrzuisccmzjk0qhv5h.jpeg", "profile_background_image_url_https":"https://pbs.twimg.com/profile_background_images/656263432/w1zrzuisccmzjk0qhv5h.jpeg", "profile_background_tile":true, "profile_link_color":"ff00ee", "profile_sidebar_border_color":"65b0da", "profile_sidebar_fill_color":"7ac3ee", "profile_text_color":"3d1957", "profile_use_background_image":true, "profile_image_url":"http://pbs.twimg.com/profile_images/652259214774128640/o8ldmd8c_normal.jpg", "profile_image_url_https":"https://pbs.twimg.com/profile_images/652259214774128640/o8ldmd8c_normal.jpg", "profile_banner_url":"https://pbs.twimg.com/profile_banners/278162142/1441077378", "default_profile":false, "default_profile_image":false, "following":null, "follow_request_sent":null, "notifications":null }, "geo":null, "coordinates":null, "place":null, "contributors":null, "retweeted_status":{ "created_at":"fri jan 22 20:19:36 +0000 2016", "id":690629910960799700, "id_str":"690629910960799744", "text":"when boyfriend doesn't watch show #realfansgetit x1 @xfinity #ad https://t.co/e4h3zo92nv", "source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">twitter iphone</a>", "truncated":false, "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":25011208, "id_str":"25011208", "name":"sara hopkins", "screen_name":"sayhop", "location":"atlanta, ga", "url":"http://shortyawards.com/8th/sayhop", "description":"never bored || tv host/social media creator ⭐️⭐️⭐️⭐️⭐️ ig/snapchat: sayhop", "protected":false, "verified":true, "followers_count":50761, "friends_count":998, "listed_count":98, "favourites_count":13178, "statuses_count":4231, "created_at":"wed mar 18 02:31:51 +0000 2009", "utc_offset":-18000, "time_zone":"eastern time (us & canada)", "geo_enabled":true, "lang":"en", "contributors_enabled":false, "is_translator":false, "profile_background_color":"000000", "profile_background_image_url":"http://pbs.twimg.com/profile_background_images/260784476/twitter1.jpg", "profile_background_image_url_https":"https://pbs.twimg.com/profile_background_images/260784476/twitter1.jpg", "profile_background_tile":false, "profile_link_color":"51d4c2", "profile_sidebar_border_color":"dbcfad", "profile_sidebar_fill_color":"0f0e0b", "profile_text_color":"ad4c9e", "profile_use_background_image":true, "profile_image_url":"http://pbs.twimg.com/profile_images/688163946512007168/crqmjogp_normal.jpg", "profile_image_url_https":"https://pbs.twimg.com/profile_images/688163946512007168/crqmjogp_normal.jpg", "profile_banner_url":"https://pbs.twimg.com/profile_banners/25011208/1414548876", "default_profile":false, "default_profile_image":false, "following":null, "follow_request_sent":null, "notifications":null }, "geo":null, "coordinates":null, "place":{ "id":"5a110d312052166f", "url":"https://api.twitter.com/1.1/geo/id/5a110d312052166f.json", "place_type":"city", "name":"san francisco", "full_name":"san francisco, ca", "country_code":"us", "country":"united states", "bounding_box":{ "type":"polygon", "coordinates":[ [ [ -122.514926, 37.708075 ], [ -122.514926, 37.833238 ], [ -122.357031, 37.833238 ], [ -122.357031, 37.708075 ] ] ] }, "attributes":{ } }, "contributors":null, "is_quote_status":false, "retweet_count":4276, "favorite_count":14129, "entities":{ "hashtags":[ { "text":"realfansgetit", "indices":[ 45, 59 ] }, { "text":"ad", "indices":[ 72, 75 ] } ], "urls":[ ], "user_mentions":[ { "screen_name":"xfinity", "name":"xfinity", "id":14996251, "id_str":"14996251", "indices":[ 63, 71 ] } ], "symbols":[ ], "media":[ { "id":690629837333999600, "id_str":"690629837333999617", "indices":[ 76, 99 ], "media_url":"http://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "media_url_https":"https://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "url":"https://t.co/e4h3zo92nv", "display_url":"pic.twitter.com/e4h3zo92nv", "expanded_url":"http://twitter.com/sayhop/status/690629910960799744/video/1", "type":"photo", "sizes":{ "thumb":{ "w":150, "h":150, "resize":"crop" }, "medium":{ "w":600, "h":338, "resize":"fit" }, "small":{ "w":340, "h":191, "resize":"fit" }, "large":{ "w":1024, "h":576, "resize":"fit" } } } ] }, "extended_entities":{ "media":[ { "id":690629837333999600, "id_str":"690629837333999617", "indices":[ 76, 99 ], "media_url":"http://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "media_url_https":"https://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "url":"https://t.co/e4h3zo92nv", "display_url":"pic.twitter.com/e4h3zo92nv", "expanded_url":"http://twitter.com/sayhop/status/690629910960799744/video/1", "type":"video", "sizes":{ "thumb":{ "w":150, "h":150, "resize":"crop" }, "medium":{ "w":600, "h":338, "resize":"fit" }, "small":{ "w":340, "h":191, "resize":"fit" }, "large":{ "w":1024, "h":576, "resize":"fit" } }, "video_info":{ "aspect_ratio":[ 16, 9 ], "duration_millis":11233, "variants":[ { "bitrate":2176000, "content_type":"video/mp4", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/1280x720/oy7p8aklaqm_c2j1.mp4" }, { "bitrate":320000, "content_type":"video/mp4", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/320x180/hlqkhhrc7sk6cgr8.mp4" }, { "bitrate":832000, "content_type":"video/webm", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/640x360/fflyylagrun8vggy.webm" }, { "bitrate":832000, "content_type":"video/mp4", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/640x360/fflyylagrun8vggy.mp4" }, { "content_type":"application/x-mpegurl", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/pl/cxsu_jfcoj5wcte5.m3u8" }, { "content_type":"application/dash+xml", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/pl/cxsu_jfcoj5wcte5.mpd" } ] } } ] }, "favorited":false, "retweeted":false, "possibly_sensitive":false, "filter_level":"low", "lang":"en" }, "is_quote_status":false, "retweet_count":0, "favorite_count":0, "entities":{ "hashtags":[ { "text":"realfansgetit", "indices":[ 57, 71 ] }, { "text":"ad", "indices":[ 84, 87 ] } ], "urls":[ ], "user_mentions":[ { "screen_name":"sayhop", "name":"sara hopkins", "id":25011208, "id_str":"25011208", "indices":[ 3, 10 ] }, { "screen_name":"xfinity", "name":"xfinity", "id":14996251, "id_str":"14996251", "indices":[ 75, 83 ] } ], "symbols":[ ], "media":[ { "id":690629837333999600, "id_str":"690629837333999617", "indices":[ 88, 111 ], "media_url":"http://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "media_url_https":"https://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "url":"https://t.co/e4h3zo92nv", "display_url":"pic.twitter.com/e4h3zo92nv", "expanded_url":"http://twitter.com/sayhop/status/690629910960799744/video/1", "type":"photo", "sizes":{ "thumb":{ "w":150, "h":150, "resize":"crop" }, "medium":{ "w":600, "h":338, "resize":"fit" }, "small":{ "w":340, "h":191, "resize":"fit" }, "large":{ "w":1024, "h":576, "resize":"fit" } }, "source_status_id":690629910960799700, "source_status_id_str":"690629910960799744", "source_user_id":25011208, "source_user_id_str":"25011208" } ] }, "extended_entities":{ "media":[ { "id":690629837333999600, "id_str":"690629837333999617", "indices":[ 88, 111 ], "media_url":"http://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "media_url_https":"https://pbs.twimg.com/ext_tw_video_thumb/690629837333999617/pu/img/ofj5psogsynefono.jpg", "url":"https://t.co/e4h3zo92nv", "display_url":"pic.twitter.com/e4h3zo92nv", "expanded_url":"http://twitter.com/sayhop/status/690629910960799744/video/1", "type":"video", "sizes":{ "thumb":{ "w":150, "h":150, "resize":"crop" }, "medium":{ "w":600, "h":338, "resize":"fit" }, "small":{ "w":340, "h":191, "resize":"fit" }, "large":{ "w":1024, "h":576, "resize":"fit" } }, "source_status_id":690629910960799700, "source_status_id_str":"690629910960799744", "source_user_id":25011208, "source_user_id_str":"25011208", "video_info":{ "aspect_ratio":[ 16, 9 ], "duration_millis":11233, "variants":[ { "bitrate":2176000, "content_type":"video/mp4", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/1280x720/oy7p8aklaqm_c2j1.mp4" }, { "bitrate":320000, "content_type":"video/mp4", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/320x180/hlqkhhrc7sk6cgr8.mp4" }, { "bitrate":832000, "content_type":"video/webm", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/640x360/fflyylagrun8vggy.webm" }, { "bitrate":832000, "content_type":"video/mp4", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/vid/640x360/fflyylagrun8vggy.mp4" }, { "content_type":"application/x-mpegurl", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/pl/cxsu_jfcoj5wcte5.m3u8" }, { "content_type":"application/dash+xml", "url":"https://video.twimg.com/ext_tw_video/690629837333999617/pu/pl/cxsu_jfcoj5wcte5.mpd" } ] } } ] }, "favorited":false, "retweeted":false, "possibly_sensitive":false, "filter_level":"low", "lang":"en", "timestamp_ms":"1454636187971" }
you need tweak how access elements. have used json
provided, stored in file , used jars jackson-core@2.7.0
, jackson-annotation@2.7.0
, jackson-databind@2.7.0
here maven repo , imported them project build path
in eclipse external jars
.
i tweaked access follow full_name
:
//get full name string fullname = root.path("retweeted_status").path("place").path("full_name").astext();
and follow coordinates
//get coordinate array jsonnode coordinates = root.path("retweeted_status").path("place").path("bounding_box").path("coordinates").get(0);
and access coordinates whole follow:
//print whole array of array system.out.println(coordinates.tostring()); //output: [[-122.514926,37.708075],[-122.514926,37.833238],[-122.357031,37.833238],[-122.357031,37.708075]]
to access coordinates individually, follow:
//print individual arrays, each lat , long system.out.println(coordinates.get(0)); //output: [-122.514926,37.708075] system.out.println(coordinates.get(1)); //output: [-122.514926,37.833238] system.out.println(coordinates.get(2)); //output: [-122.357031,37.833238] system.out.println(coordinates.get(3)); //output: [-122.357031,37.708075]
to access each coordinates lat
, long
separately following:
//print lat , long of each array seperately system.out.println(coordinates.get(0).get(0)); //output: -122.514926 system.out.println(coordinates.get(0).get(1)); //output: 37.708075 system.out.println(coordinates.get(1).get(0)); system.out.println(coordinates.get(1).get(1)); system.out.println(coordinates.get(2).get(0)); system.out.println(coordinates.get(2).get(1)); system.out.println(coordinates.get(3).get(0)); system.out.println(coordinates.get(3).get(1));
here whole code, used testing purpose, reading json file:
import java.io.file; import java.io.ioexception; import com.fasterxml.jackson.core.jsonparseexception; import com.fasterxml.jackson.databind.jsonnode; import com.fasterxml.jackson.databind.objectmapper; public class jacksontest { public static void main(string[] args) throws jsonparseexception, ioexception { file file = new file("c:\\test_java\\jackson.json"); objectmapper mapper = new objectmapper(); jsonnode root = mapper.readtree(file); //get full name string fullname = root.path("retweeted_status").path("place").path("full_name").astext(); system.out.println("full_name: " + fullname); //get coordinate array jsonnode coordinates = root.path("retweeted_status").path("place").path("bounding_box").path("coordinates").get(0); //print whole array of array system.out.println(coordinates.tostring()); //print individual arrays, each lat , long system.out.println(coordinates.get(0)); system.out.println(coordinates.get(1)); system.out.println(coordinates.get(2)); system.out.println(coordinates.get(3)); //print lat , long of each array seperately system.out.println(coordinates.get(0).get(0)); system.out.println(coordinates.get(0).get(1)); system.out.println(coordinates.get(1).get(0)); system.out.println(coordinates.get(1).get(1)); system.out.println(coordinates.get(2).get(0)); system.out.println(coordinates.get(2).get(1)); system.out.println(coordinates.get(3).get(0)); system.out.println(coordinates.get(3).get(1)); } }
and here output of above code when run:
full_name: san francisco, ca
[[-122.514926,37.708075],[-122.514926,37.833238],[-122.357031,37.833238],[-122.357031,37.708075]] [-122.514926,37.708075] [-122.514926,37.833238]
[-122.357031,37.833238] [-122.357031,37.708075]
-122.514926
37.708075
-122.514926
37.833238
-122.357031
37.833238
-122.357031
37.708075
Comments
Post a Comment