C#でJsonを扱う DynamicJson編
・C#でJsonを扱う DynamicJson編
・C#でJsonを扱う Json.Net JToken/JObject/JArray編
・C#でJsonを扱う Json.Net DeserializeObject編
・C#でJsonを扱う System.Text.Json編
DynamicJsonについて
neue cc – DynamicJson – C# 4.0のdynamicでスムーズにJSONを扱うライブラリ
App.xaml
<Application x:Class="WpfApp13.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp13"
StartupUri="ViewMainView.xaml">
<Application.Resources>
</Application.Resources>
</Application>
MainView.xaml
<Window x:Class="WpfApp13.View.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
xmlns:local="clr-namespace:WpfApp13.View"
xmlns:vm="clr-namespace:WpfApp13.ViewModel"
mc:Ignorable="d"
Title="MainView" Height="200" Width="300">
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<i:Interaction.Triggers>
<i:EventTrigger EventName ="ContentRendered">
<l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="OnContentRendered" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid ItemsSource="{Binding arrayWeather}">
</DataGrid>
</Window>
MainViewModel.cs
using Codeplex.Data;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
namespace WpfApp13.ViewModel
{
public class MainViewModel : Livet.ViewModel
{
private ObservableCollection<Entity.WeatherEntity> _arrayWeather;
public ObservableCollection<Entity.WeatherEntity> arrayWeather
{
get
{
return _arrayWeather;
}
set
{
_arrayWeather = value;
RaisePropertyChanged();
}
}
public void OnContentRendered()
{
string strSource = "";
using (System.Net.WebClient objWebClient = new System.Net.WebClient())
{
// JSONデータの取得
strSource = objWebClient.DownloadString("http://weather.livedoor.com/forecast/webservice/json/v1?city=130010");
}
// Unicodeエスケープ文字を元に戻す
strSource = Regex.Unescape(strSource);
strSource = strSource.Replace("
", "");
// DynamicJsonでパース
dynamic dynJson = DynamicJson.Parse(strSource);
dynamic dynForecasts = dynJson.forecasts;
List<Entity.WeatherEntity> lstWeather = new List<Entity.WeatherEntity>();
// 配列か否かの判定はIsArray
if(dynForecasts.IsArray == true)
{
foreach(dynamic dynData in dynForecasts)
{
Entity.WeatherEntity objWeatherEntity = new Entity.WeatherEntity();
// 定義の有無はIsDefined
if (dynData.IsDefined("dateLabel"))
{
// インデクサでアクセス
objWeatherEntity.DataLabel = dynData["dateLabel"];
}
// プロパティを直接指定してアクセス
objWeatherEntity.Telop = dynData.telop;
objWeatherEntity.Date = dynData.date;
lstWeather.Add(objWeatherEntity);
}
}
arrayWeather = new ObservableCollection<Entity.WeatherEntity>(lstWeather);
}
}
}
WeatherEntity.cs
namespace WpfApp13.Entity
{
public class WeatherEntity
{
public string DataLabel { get; set; }
public string Telop { get; set; }
public string Date { get; set; }
}
}
ディスカッション
コメント一覧
まだ、コメントがありません