2014-02-08 14 views
1

我一直在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文件。

請原諒牆上的文字。

回答

1

它可以與Gson庫非常簡單。

因爲,你已經通過查看創建完成一個POJO,辛勤工作的json你可以驗證User是一個有效的json對象,而不是String值。

因此,讓我們修改POJO的(Tweetuser屬性稍微有:

User user; 

public User getUser() { 
    return user; 
} 

public void setUser(User user) { 
    this.user = user; 
} 

User自定義類是:

public class User { 
    private String id; 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 
} 

現在只需撥打你的GSON方法的對象轉換從json(我將json存儲到一個文件並通過BufferedReader讀取):

public static void main(String[] args) throws FileNotFoundException { 
     Gson gson = new Gson(); 
     BufferedReader br = new BufferedReader(new FileReader(
       "json.txt")); 

     Tweet tweetObj = gson.fromJson(br, Tweet.class); 
     System.out.println(tweetObj.getUser().getId()); 
    } 

輸出:

366301747 

編輯:根據意見,解決方案使用 - 傑克遜2個選項

  1. 保持新創建User類中,ObjectMapper代碼保持完全相同, System.out.println(tweet.getUser().getId())讓你的用戶名。
  2. 如果不使用User類,改變你的Tweet看起來像這樣:

代碼:

public class Tweet { 

    String created_at; 
    String id; 
    String text; 
    Map<String, String> user; 

    public String getCreated_at() { 
     return created_at; 
    } 

    public void setCreated_at(String created_at) { 
     this.created_at = created_at; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 

    public Map<String, String> getUser() { 
     return user; 
    } 

    public void setUser(Map<String, String> user) { 
     this.user = user; 
    } 

} 

而在調用方法打印userid爲:

ObjectMapper mapper = new ObjectMapper(); 
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 

Tweet tweet = mapper.readValue(br, Tweet.class); 

System.out.println(tweet.getUser().get("id")); 

取得:

366301747 
+0

我明白你的意思,它看起來像一個可接受的想法。但有沒有什麼辦法,我可以做到這一點沒有類用戶?我的意思是,我可以直接從我的Tweet對象獲取用戶ID嗎?這是否有機會與傑克遜合作?如果我很困難,我會請求原諒。 – ViRALiC

+0

你先生,應該得到超過一個啤酒。我真的很感激,因爲我坐了幾個小時沒有得到這個。它現在有效。你讓我整整一天! – ViRALiC

+0

@ViRALiC很樂意幫助:) – PopoFibo

1

您可以更改setUser方法採取Map和手動設置user.id

public void setUser(Map<String, Object> props) { 
    user = props.get("id").toString(); 
} 
+0

我對Map有點新,所以你會關心我將如何利用上面的json字符串的setUser方法?在我的場景中,我會爲這種方法使用什麼參數?原諒這個新手問題。 – ViRALiC

+0

基本上當你有'setUser(地圖道具)'方法,那麼'道具'將是一個地圖'{「id」:366301747,「id_str」:「366301747」,「name」:「skinny-pete」,... '。然後您可以提取json'id'字段並將其分配給'this.user'。 'user = props.get(「id」)。toString()'確實如此。而且由於你使用的是Jackson,你不需要自己將參數傳遞給'setUser'。當你執行mapper.readValue(...)時,Jackson會在幕後調用'setUser'方法。 –

+0

非常感謝你的解釋。你的答案是最有用的! – ViRALiC