Created
July 24, 2012 16:15
-
-
Save ishisaka/3170967 to your computer and use it in GitHub Desktop.
某アンケート回答の回答取得API使用例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Net; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace JsonTest1 | |
{ | |
class Program | |
{ | |
/// <summary> | |
/// 標準になったくさいJson.NETを使用して、Code 2012のアンケート結果を取得する。 | |
/// </summary> | |
/// <param name="args">コマンド引数。しかし使用しない!</param> | |
static void Main(string[] args) { | |
//アンケート回答取得APIのURL | |
var url = @"http://code-survey.herokuapp.com/surveys.json"; | |
//APIをたたいてJsonでアンケートの回答を取得する。 | |
var clinet = new WebClient(); | |
var response = clinet.DownloadString(new Uri(url)); | |
//Json.NETを使って、回答をSurveyクラスのリストに変換! | |
var surveyList = JsonConvert.DeserializeObject<List<Survey>>(response); | |
//内容表示 | |
foreach (Survey _survey in surveyList) { | |
Console.WriteLine("id: {0}" , _survey.id ?? 0); | |
Console.WriteLine("app_name: {0}", _survey.app_name ?? ""); | |
Console.WriteLine("created_at: {0}", _survey.created_at ?? DateTime.MinValue); | |
Console.WriteLine("updated_at: {0}", _survey.updated_at ?? DateTime.MinValue); | |
Console.WriteLine("how_year: {0}", _survey.how_year ?? 0); | |
Console.WriteLine("locale: {0}", _survey.locale ?? ""); | |
Console.WriteLine("languages: {0}", _survey.languages ?? ""); | |
Console.WriteLine("free_comment: \r\n{0}", _survey.free_comment ?? ""); | |
Console.WriteLine("why: {0}", _survey.why ?? ""); | |
Console.WriteLine("=============================================================="); | |
} | |
Console.ReadLine(); | |
} | |
} | |
/// <summary> | |
/// Code 2012 アンケート回答 | |
/// </summary> | |
public class Survey | |
{ | |
public string app_name { get; set; } | |
public DateTime? created_at { get; set; } | |
public string free_comment { get; set; } | |
public int? how_year { get; set; } | |
public int? id { get; set; } | |
public string languages { get; set; } | |
public string locale { get; set; } | |
public DateTime? updated_at { get; set; } | |
public string why { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment