だいぶ前、VB.net で同じような事したんですが、だいぶ変わっていたし、全部見て無いですが、対象によって違うかもしれません。これは Windows Phone の C# です(Windows8 のC# でも動作しました)。 JSON データの整形は、http://winofsql.jp/php/cnvtext/frame.htm から、『JavaScript整形』で可能です。 ※ DynamicJson は Windows Phone の環境で使えません。
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Diagnostics; // PropertyChangedEventHandler using System.ComponentModel; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace PhoneApp1 { public class Class1 : INotifyPropertyChanged { public Class1(){ WebClient webClient = new WebClient(); webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted); // 外部サービスから文字列を取得 webClient.DownloadStringAsync(new System.Uri("http://textt.net/sworc/20120924055943.txt")); } // ページ名プロパティ( バインド用 ) public string pageName { get; set; } private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { Deployment.Current.Dispatcher.BeginInvoke(() => { Debug.WriteLine(e.Error.Message); }); } else { // オーソドックスなデシリアライズ Class2 JsonObject = JsonConvert.DeserializeObject<Class2>(e.Result); // 定義済のクラスとして参照( 未定義のプロパティは無視 ) Debug.WriteLine(JsonObject.id); Debug.WriteLine(JsonObject.profile_background_image_url_https); // 文字列から JObject を作成 JObject jo = JObject.Parse(e.Result); // JObject で参照 Debug.WriteLine(jo["id"]); Debug.WriteLine(jo["profile_background_image_url_https"]); // JToken で参照 foreach (JToken jt in jo.Children()) { Debug.WriteLine("-->" + jt.ToString()); Debug.WriteLine("-->" + jt.First); } // JProperty で参照 foreach (JProperty jp in jo.Properties()) { Debug.WriteLine( jp.Name + "-->" + jp.Value.ToString()); } } } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace PhoneApp1 { public class Class2 { public int id { get; set; } public string profile_background_image_url_https { get; set; } } }
|
【C#の最新記事】
- C# : PHP と連携してバイナリデータを WebClient.UploadDataAsync でそのままアップロードする
- C# : WebClient で JSON データを取得後 Json.NET でオブジェクト化( ついでに PropertyInfo でプロパティデータ一覧を foreach で取得 )
- C# : Microsoft Access の接続で、他の RDBMS( ここでは MySQL ) にエクスポートを行う
- C# : TKMP.DLLを使った、Gmail 用メール送信テンプレート
- C# : DataGridView を使用したナチュラルな行データの更新
- C# の delegate : メソッドの引数にメソッドを渡して使用する方法と、JavaScript の function(){} と同じ使用方法( 匿名 )
- PowerShell 移行用 C# コンソールアプリのコードテスト( ZIP 圧縮 )
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (4)
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (3)
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (2)
- 解く事が目的では無い、身に付ける事が目的の C# 初心者用の問題を作ったので良かったらどうぞ (1)
- ComboBox : C# : Form アプリケーションで良く使うコントロール / VS2012 にて
- C# バッチビルドキットで、VB の My 名前空間を使ってクリップボードを使ったり、キーボードの SHIFT キーが押されているかを知る
- printui.dll を VB.net または C# から呼び出す/ DllImport で LoadLibraryA / VS2010
- LINQ で List
のソート - VS2010(C#)WPF : ListView 内に GridView を配置する
- Windows ストア用の System.Collections.Generic 名前空間には、SortedList はありません。
- 情報が全くみつからない Microsoft.Live
- VS2010(C#) バッチ(コンソールアプリ) Twitter 投稿
- Windows8(C#) の WebView の LoadComplete で取得した URL 内のアクセストークンを Split で取得