在C#中获取Twitter个人资料数据

时间:2012-06-15 06:23:50

标签: c# json c#-4.0 twitter

我正在C#中创建一个应用程序,用户输入他们的名字,并从Twitter收到相应的个人资料数据。我现在不使用API​​。如果我使用:

https://api.twitter.com/1/users/show.json?screen_name=kool_aid_wino

响应是这样的JSON消息。

{"id":184937673,"id_str":"184937673","name":"Brautigan's Ghost","screen_name":"Kool_Aid_Wino","location":"","description":"Author Richard Brautigan floating through time. Fan Account.","url":null,"protected":false,"followers_count":1803,"friends_count":623,"listed_count":97,"created_at":"Mon Aug 30 21:21:12 +0000 2010","favourites_count":0,"utc_offset":-18000,"time_zone":"Quito","geo_enabled":false,"verified":false,"statuses_count":653,"lang":"en","status":{"created_at":"Fri Jun 15 00:47:15 +0000 2012","id":213432448762134528,"id_str":"213432448762134528","text":"I sat beside my little brother on the front porch, and I told him a story about a flower that fell in love with a star.","source":"\u003ca href=\"http:\/\/twitterrific.com\" rel=\"nofollow\"\u003eTwitterrific\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,"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":11,"favorited":false,"retweeted":false},"contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1114076851\/Brautigan_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1114076851\/Brautigan_normal.jpg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"show_all_inline_media":false,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null}

我脑子里的询问很少:

  • 我是否需要在C#中使用JSON解析器来浏览JSON消息?
  • 此JSON消息不包含有关推广推文的信息,我该如何获得?

2 个答案:

答案 0 :(得分:1)

  

我是否需要在C#中使用JSON解析器来浏览JSON消息?

嗯,从技术上讲,答案是肯定的,但我假设你在问你是否需要第三方。 C#具有JavaScriptSerializer类,只要您首先使用适当的属性定义类,它就可以使用此JSON并将其转换为对象。

答案 1 :(得分:1)

以下是Twitter's REST API的文档。 您可以按如下方式使用它们:

using (var wc = new WebClient())
{
    string searchText = "kool_aid_wino";
    string json = wc.DownloadString("http://search.twitter.com/search.json?rpp=100&q="+searchText);
    dynamic dynJson = JsonConvert.DeserializeObject(json);
    foreach (var result in dynJson.results)
    {
        Console.WriteLine("{0} --> {1} : {2}\n", result.from_user, result.to_user, result.text);
    }
}

PS:你需要Json.Net(我最喜欢的json解析器)来运行代码