C# で JSON 形式のデータを取得することが VS2010 + .NET Framework 4.0 で非常に簡単に実現できるようになりました。System.Web.Extensions.dll で提供されている JavaScriptSerializer クラスを利用するのが便利です。
(※ System.Web.Extensions.dll は参照を追加する必要があります。)
郵便番号変換サービスを利用して、早速、試してみました。郵便番号の住所を JSON 形式で取得するオプションを指定して、HTTP GET で結果を文字列として受け取ります。それを YubinResponse0 というクラスに逆シリアライズするために JavaScriptSerializer クラスの Deserialize を呼ぶだけです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
namespace yubin7
{
// https://www.northwind.mydns.jp/samples/webservice/getaddress/1230841/<option>/json/utf8
// <option>
// 0: [{"seq":1,"zip7":"123-0841","addr":"u6771u4eacu90fdu8db3u7acbu533au897fu65b0u4e95"}]
// 2: [{"seq":1,"zip7":"123-0841","pref":"u6771u4eacu90fd","addr":"u8db3u7acbu533au897fu65b0u4e95"}]
// 3: [{"seq":1,"zip7":"123-0841","pref":"u6771u4eacu90fd","city":"u8db3u7acbu533a","town":"u897fu65b0u4e95"}]
//
// See https://www.northwind.mydns.jp/samples/webservice/webapidoc.php for more info.
public class YubinResponse0
{
public string seq { get; set; }
public string zip7 { get; set; }
public string addr { get; set; }
}
public class YubinResponse2
{
public string seq { get; set; }
public string zip7 { get; set; }
public string pref { get; set; }
public string addr { get; set; }
}
public class YubinResponse3
{
public string seq { get; set; }
public string zip7 { get; set; }
public string pref { get; set; }
public string city { get; set; }
public string town { get; set; }
}
public class Yubin7
{
const string YUBIN_API = "https://www.northwind.mydns.jp/samples/webservice/getaddress/{0}/0/json/utf8";
public Yubin7()
{
}
public string getAddress(string yubinNo)
{
string uri = string.Format(YUBIN_API, yubinNo);
string result = httpGet(uri, "");
string addr = "";
if (result != null && result.Length > 0)
{
try
{
// JSON 形式データを変換
JavaScriptSerializer ser = new JavaScriptSerializer();
List<YubinResponse0> res0 = ser.Deserialize<List<YubinResponse0>>(result);
foreach (YubinResponse0 r in res0)
{
addr += r.addr + "\r\n";
}
}
catch (Exception e)
{
System.Diagnostics.Debug.Print(e.ToString());
}
}
return addr;
}
public bool chkYubinNo(string yubinNo)
{
return (System.Text.RegularExpressions.Regex.IsMatch(
yubinNo,
@"^\d{3}-\d{4}$",
System.Text.RegularExpressions.RegexOptions.ECMAScript));
}
string httpGet(string URI, string Parameters)
{
string result = "";
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Method = "GET";
System.Net.WebResponse resp = req.GetResponse();
if (resp == null)
{
return "";
}
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
result = sr.ReadToEnd().Trim();
}
catch (Exception e)
{
System.Diagnostics.Debug.Print(e.ToString());
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("郵便番号を入力して Enter キーを押下してください。");
Console.WriteLine("終了するには、なにも入力しないで Enter キーを押下してください。");
Console.Write("郵便番号: ");
string sin = Console.ReadLine();
Yubin7 yubin7 = new Yubin7();
while (sin != null && sin.Length > 0)
{
if (yubin7.chkYubinNo(sin))
{
sin = sin.Replace("-", "");
string result = yubin7.getAddress(sin);
if (result.Length > 0)
{
Console.WriteLine(result);
}
else
{
Console.WriteLine("該当なし");
}
}
else
{
Console.WriteLine("郵便番号は7桁で 123-4567 の形式で入力してください。");
}
Console.Write("郵便番号: ");
sin = Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}