前回は、twitterの最新のつぶやきを表示する、
簡単なアプリケーションを作成してみました。
ロジックとしては、twitterで指定されたAPIを呼び出し、
XML形式のデータを取得し、XPathでXMLデータを解析して
表示するといった内容でした。
今回は、Webの世界ではXMLより手軽に扱えるとされている、
JSON形式のデータを扱うことにします。twitterのAPIでも標準で
JSON形式のデータに対応されています。
今回も、C#.netを使用してアプリケーションを作成してみました。
(プログラムの機能は前回と同じです)
- C#.net 2008
- .net framework2.0
- Json.NET 3.5 beta4

※実行にはJson.NETのライブラリも必要です(ダウンロード)
JSON形式のデータをC#で定義したクラスにマッピングすることで、
マッピング後のデータの操作が簡単・柔軟になります。
ソース(TwitterForm.cs)
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json;
namespace TwitterApplication
{
public partial class TwitterForm : Form
{
public TwitterForm()
{
InitializeComponent();
lstItems.Items.Clear();
}
private String GetTweet(String URL)
{
//Request
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.Method = "GET";
WebResponse Response = Request.GetResponse();
//Read
StreamReader Readar = new StreamReader(Response.GetResponseStream());
String Results = Readar.ReadToEnd();
//Return
Readar.Close();
Response.Close();
return Results;
}
private void btnTimeLine_Click(object sender, EventArgs e)
{
String URL = "http://twitter.com/statuses/user_timeline/" + txtUser.Text + ".json?count=50";
String JSON = GetTweet(URL);
List<Status> StatusList = (List<Status>)JsonConvert.DeserializeObject(JSON, typeof(List<Status>));
lstItems.Items.Clear();
lstItems.HorizontalScrollbar = false;
foreach (Status StatusObject in StatusList)
{
lstItems.Items.Add(StatusObject.Text);
}
//ScrollBarSetting
lstItems.HorizontalScrollbar = true;
}
}
}
ソース(Twitter.cs)
namespace TwitterApplication
{
public sealed class Status
{
public string In_reply_to_status_id { get; set; }
public bool Favorited { get; set; }
public string In_reply_to_user_id { get; set; }
public string Source { get; set; }
public string Created_at { get; set; }
public string In_reply_to_screen_name { get; set; }
public User User { get; set; }
public string Id { get; set; }
public bool Truncated { get; set; }
public string Text { get; set; }
}
public sealed class User
{
public string Profile_sidebar_border_color { get; set; }
public string Description { get; set; }
public string Url { get; set; }
public string Screen_name { get; set; }
public string Following { get; set; }
public bool Verified { get; set; }
public string Profile_text_color { get; set; }
public int Followers_count { get; set; }
public string Profile_background_image_url { get; set; }
public string Created_at { get; set; }
public string Notifications { get; set; }
public int Friends_count { get; set; }
public string Profile_link_color { get; set; }
public bool Profile_background_tile { get; set; }
public int Favourites_count { get; set; }
public string Profile_background_color { get; set; }
public bool Protected { get; set; }
public string Time_zone { get; set; }
public string Location { get; set; }
public string Name { get; set; }
public string Profile_sidebar_fill_color { get; set; }
public string Id { get; set; }
public int Statuses_count { get; set; }
public int Utc_offset { get; set; }
public string Profile_image_url { get; set; }
}
}
コメントする