2016-02-05 169 views
2

我遇到了一个问题,其中我的逻辑无法从Twitter源中检索JSON数组中的前两个坐标。我正在使用Jackson JsonNode来检索Twitter Feed。无法从Twitter地理代码中检索地点坐标

下面是我首先验证节点不为空的逻辑,如果它不为空,那么我尝试从平面中检索前两个lon/lat坐标。

问题是它再也不能挽回的转推的坐标 - >地点 - > Bounding_box - >坐标 - >得0 0 &得到0 1

源代码逻辑

   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(); 
        } 
       } 

我科我试图在bounding_box数组中检索,前两个地理代码

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

样品JSON

{ 
    "created_at":"Fri Feb 05 01:36:27 +0000 2016", 
    "id":695420692209111000, 
    "id_str":"695420692209111040", 
    "text":"RT @Sayhop: When your boyfriend doesn't watch your show #RealFansGetIt X1 @XFINITY #ad https://t.co/E4H3zO92nV", 
    "source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for 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 your boyfriend doesn't watch your show #RealFansGetIt X1 @XFINITY #ad https://t.co/E4H3zO92nV", 
     "source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for 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" 
} 

回答

0

你需要调整你如何访问元素。我已经使用你提供的JSON,将它存储在一个文件中,并使用来自这里的[email protected],[email protected],[email protected]的罐子,并将它们导入到我的项目build path中,在日食中作为external jars

我调整了接入为遵循full_name

//Get the full name 
String fullName = root.path("retweeted_status").path("place").path("full_name").asText(); 

,并按照coordinates

//Get the coordinate array 
JsonNode coordinates = root.path("retweeted_status").path("place").path("bounding_box").path("coordinates").get(0); 

,然后访问的坐标作为一个整体如下:

//print the 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]] 

要单独访问坐标,请按以下步骤操作:

//print individual arrays, where each is lat and 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] 

访问的每个坐标latlong分别然后下面:

//print lat and 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)); 

这里是整个代码,我用于测试目的,从文件中读取您的JSON:

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 the full name 
     String fullName = root.path("retweeted_status").path("place").path("full_name").asText(); 

     System.out.println("full_name: " + fullName); 

     //Get the coordinate array 
     JsonNode coordinates = root.path("retweeted_status").path("place").path("bounding_box").path("coordinates").get(0); 

     //print the whole array of array 
     System.out.println(coordinates.toString()); 

     //print individual arrays, where each is lat and 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 and 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)); 
    } 
} 

这里是运行时上述代码的输出:

FULL_NAME:旧金山,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

0

我怀疑你的代码只需要处理额外的数组边界框

https://dev.twitter.com/overview/api/places

坐标:浮法

数组的数组的数组.. 。

边界框数组a重新包装在一个附加阵列中以与多边形符号兼容。

+0

我还试图root.path( “retweeted_status”)。路径( “地点”)。 。路径( “bounding_box”)路径( “坐标”)得到(0)获得(0)获得(0).textValue()。它仍然返回null。 – user2100493

+0

你将不得不调试这个。把一个中断点和逐步评估每一步。或者System.out.println每个连续的值。 –