我一直在Eclipse中玩弄推文一段時間,這是作爲一個JSON字符串呈現。來自json字符串的具體信息
爲此,我創建了一個叫做分享Tweet的對象(原始的,是吧?),這需要從JSON字符串的某些信息,並將其存儲在鳴叫對象。沒有什麼花哨。
我分享Tweet類看起來如下:
public class Tweet implements TwitterMelding {
public Tweet() {
}
String created_at;
String id;
String text;
String user;
public void setUser(String user) {
this.user = user;
}
public void setText(String text) {
this.text = text;
}
public void setId(String id) {
this.id = id;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
}
現在,簡單,因爲它可能看起來,還有那些不工作之一。
具體地說字符串用戶。它應該做的是發佈推文的人的商店用戶ID。
以下是作爲鳴叫從Twitter在所有獲得它的可怕長度:
{"created_at":"Sat Feb 08 15:37:37 +0000 2014","id":432176397474623489,"id_str":"432176397474623489","text":"Skal begynne \u00e5 selge vekter.. Eneste m\u00e5ten det konstant kommer penger i lommen","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","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":366301747,"id_str":"366301747","name":"skinny-pete","screen_name":"JFarsund","location":"bj\u00f8rge","url":null,"description":"j\u00f8rgen er en tynn gutt med pack.. Men det teller vel ikke? Det gj\u00f8r vel ikke bio heller","protected":false,"followers_count":427,"friends_count":291,"listed_count":2,"created_at":"Thu Sep 01 23:03:49 +0000 2011","favourites_count":5103,"utc_offset":3600,"time_zone":"Copenhagen","geo_enabled":true,"verified":false,"statuses_count":8827,"lang":"no","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/366301747\/1391822743","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":{"type":"Point","coordinates":[60.33700829,5.24626808]},"coordinates":{"type":"Point","coordinates":[5.24626808,60.33700829]},"place":{"id":"2260fcb4a77f2bad","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/2260fcb4a77f2bad.json","place_type":"city","name":"Bergen","full_name":"Bergen, Hordaland","country_code":"NO","country":"Norge","contained_within":[],"bounding_box":{"type":"Polygon","coordinates":[[[5.1602697,60.1848543],[5.1602697,60.5335445],[5.6866852,60.5335445],[5.6866852,60.1848543]]]},"attributes":{}},"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"no"}
這真的是一個很長眼痛。
我增加了一些「......」到下一個,讓它稍微更具可讀性,只顯示我感興趣的值:
{…,"user":{"id":366301747,"id_str":"366301747","name":"skinny-pete","screen_name":"JFarsund","location":"bj\u00f8rge","url":null,"description":"j\u00f8rgen er en tynn gutt med pack.. Men det teller vel ikke? Det gj\u00f8r vel ikke bio heller","protected":false,"followers_count":427,"friends_count":291,"listed_count":2,"created_at":"Thu Sep 01 23:03:49 +0000 2011","favourites_count":5103,"utc_offset":3600,"time_zone":"Copenhagen","geo_enabled":true,"verified":false,"statuses_count":8827,"lang":"no","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000089578611\/6840970475350d63190eb05d3d7e47ec.png","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/431961396528414720\/EwkxQBkW_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/366301747\/1391822743","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null}, …}
權,還是和我在一起?
正如我上面提到的,我想是用戶ID,這是我要分配給在鳴叫對象變量「用戶」。
{…,"user":{"id":366301747,"id_str":"366301747",… }…}
所有我想要的是指定的號碼,在我分享Tweet對象變量「用戶」。
但對我的生活,我似乎無法。
爲了確保鳴叫對象得到它想要的信息,而不是它不,我使用的是傑克遜對象信息:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
所以我的問題。
如何告訴我從我的json字符串中取數字並將其分配給變量「user」?
我寧願它是可行的與傑克遜獨自一人,並沒有超過必要進口更多的JAR文件。
請原諒牆上的文字。
我明白你的意思,它看起來像一個可接受的想法。但有沒有什麼辦法,我可以做到這一點沒有類用戶?我的意思是,我可以直接從我的Tweet對象獲取用戶ID嗎?這是否有機會與傑克遜合作?如果我很困難,我會請求原諒。 – ViRALiC
你先生,應該得到超過一個啤酒。我真的很感激,因爲我坐了幾個小時沒有得到這個。它現在有效。你讓我整整一天! – ViRALiC
@ViRALiC很樂意幫助:) – PopoFibo