在 C# 中获取推特配置文件数据

本文关键字:配置文件 数据 获取 | 更新日期: 2023-09-27 18:37:21

我正在用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 消息不包含有关推广推文的信息,我该如何获取?

在 C# 中获取推特配置文件数据

以下是 Twitter 的 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解析器)才能运行上面的代码

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

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