{"id":527,"date":"2019-12-09T00:29:21","date_gmt":"2019-12-08T15:29:21","guid":{"rendered":"http:\/\/marius.main.jp\/software\/blog\/?p=527"},"modified":"2020-06-07T17:30:19","modified_gmt":"2020-06-07T08:30:19","slug":"post-527","status":"publish","type":"post","link":"https:\/\/todosoft.net\/blog\/?p=527","title":{"rendered":"C#\u3067Json\u3092\u6271\u3046 DynamicJson\u7de8"},"content":{"rendered":"\n<p>\u30fb<a href=\"https:\/\/todosoft.net\/blog\/?p=527\">C#\u3067Json\u3092\u6271\u3046 DynamicJson\u7de8<\/a><br>\u30fb<a href=\"https:\/\/todosoft.net\/blog\/?p=674\">C#\u3067Json\u3092\u6271\u3046 Json.Net JToken\/JObject\/JArray\u7de8<\/a><br>\u30fb<a href=\"https:\/\/todosoft.net\/blog\/?p=683\">C#\u3067Json\u3092\u6271\u3046 Json.Net DeserializeObject\u7de8<\/a><br>\u30fbC#\u3067Json\u3092\u6271\u3046 System.Text.Json\u7de8<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DynamicJson\u306b\u3064\u3044\u3066<\/h2>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\"neue cc - DynamicJson - C# 4.0\u306edynamic\u3067\u30b9\u30e0\u30fc\u30ba\u306bJSON\u3092\u6271\u3046\u30e9\u30a4\u30d6\u30e9\u30ea (\u65b0\u3057\u3044\u30bf\u30d6\u3067\u958b\u304f)\" href=\"http:\/\/neue.cc\/2010\/04\/30_256.html\" target=\"_blank\">neue cc &#8211; DynamicJson &#8211; C# 4.0\u306edynamic\u3067\u30b9\u30e0\u30fc\u30ba\u306bJSON\u3092\u6271\u3046\u30e9\u30a4\u30d6\u30e9\u30ea<\/a><\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">App.xaml<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">&lt;Application x:Class=\"WpfApp13.App\"\n             xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n             xmlns:local=\"clr-namespace:WpfApp13\"\n             StartupUri=\"ViewMainView.xaml\">\n    &lt;Application.Resources>\n         \n    &lt;\/Application.Resources>\n&lt;\/Application><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">MainView.xaml<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">&lt;Window x:Class=\"WpfApp13.View.MainView\"\n        xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n        xmlns:d=\"http:\/\/schemas.microsoft.com\/expression\/blend\/2008\"\n        xmlns:mc=\"http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006\"\n        xmlns:i=\"http:\/\/schemas.microsoft.com\/xaml\/behaviors\"\n        xmlns:l=\"http:\/\/schemas.livet-mvvm.net\/2011\/wpf\"\n        xmlns:local=\"clr-namespace:WpfApp13.View\"\n        xmlns:vm=\"clr-namespace:WpfApp13.ViewModel\"\n        mc:Ignorable=\"d\"\n        Title=\"MainView\" Height=\"200\" Width=\"300\">\n\n    &lt;Window.DataContext>\n        &lt;vm:MainViewModel\/>\n    &lt;\/Window.DataContext>\n\n    &lt;i:Interaction.Triggers>\n        &lt;i:EventTrigger EventName =\"ContentRendered\">\n            &lt;l:LivetCallMethodAction MethodTarget=\"{Binding}\" MethodName=\"OnContentRendered\" \/>\n        &lt;\/i:EventTrigger>\n    &lt;\/i:Interaction.Triggers>\n\n    &lt;DataGrid ItemsSource=\"{Binding arrayWeather}\">\n\n    &lt;\/DataGrid>\n&lt;\/Window><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">MainViewModel.cs<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">using Codeplex.Data;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Text.RegularExpressions;\n\nnamespace WpfApp13.ViewModel\n{\n    public class MainViewModel : Livet.ViewModel\n    {\n        private ObservableCollection&lt;Entity.WeatherEntity> _arrayWeather;\n        public ObservableCollection&lt;Entity.WeatherEntity> arrayWeather\n        {\n            get\n            {\n                return _arrayWeather;\n            }\n            set\n            {\n                _arrayWeather = value;\n                RaisePropertyChanged();\n            }\n        }\n\n        public void OnContentRendered()\n        {\n            string strSource = \"\";\n\n            using (System.Net.WebClient objWebClient = new System.Net.WebClient())\n            {\n                \/\/ JSON\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\n                strSource = objWebClient.DownloadString(\"http:\/\/weather.livedoor.com\/forecast\/webservice\/json\/v1?city=130010\");\n            }\n\n            \/\/ Unicode\u30a8\u30b9\u30b1\u30fc\u30d7\u6587\u5b57\u3092\u5143\u306b\u623b\u3059\n            strSource = Regex.Unescape(strSource);\n\n            strSource = strSource.Replace(\"\n\", \"\");\n\n            \/\/ DynamicJson\u3067\u30d1\u30fc\u30b9\n            dynamic dynJson = DynamicJson.Parse(strSource);\n\n            dynamic dynForecasts = dynJson.forecasts;\n\n            List&lt;Entity.WeatherEntity> lstWeather = new List&lt;Entity.WeatherEntity>();\n\n            \/\/ \u914d\u5217\u304b\u5426\u304b\u306e\u5224\u5b9a\u306fIsArray\n            if(dynForecasts.IsArray == true)\n            {\n                foreach(dynamic dynData in dynForecasts)\n                {\n                    Entity.WeatherEntity objWeatherEntity = new Entity.WeatherEntity();\n\n                    \/\/ \u5b9a\u7fa9\u306e\u6709\u7121\u306fIsDefined\n                    if (dynData.IsDefined(\"dateLabel\"))\n                    {\n                        \/\/ \u30a4\u30f3\u30c7\u30af\u30b5\u3067\u30a2\u30af\u30bb\u30b9\n                        objWeatherEntity.DataLabel = dynData[\"dateLabel\"];\n                    }\n\n                    \/\/ \u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u76f4\u63a5\u6307\u5b9a\u3057\u3066\u30a2\u30af\u30bb\u30b9\n                    objWeatherEntity.Telop = dynData.telop;\n                    objWeatherEntity.Date = dynData.date;\n\n                    lstWeather.Add(objWeatherEntity);\n                }\n            }\n\n            arrayWeather = new ObservableCollection&lt;Entity.WeatherEntity>(lstWeather);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">WeatherEntity.cs<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">namespace WpfApp13.Entity\n{\n    public class WeatherEntity\n    {\n        public string DataLabel { get; set; }\n\n        public string Telop { get; set; }\n\n        public string Date { get; set; }\n\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30fbC#\u3067Json\u3092\u6271\u3046 DynamicJson\u7de8\u30fbC#\u3067Json\u3092\u6271\u3046 Json.Net JToken\/JObject\/JArray\u7de8\u30fbC#\u3067Json\u3092\u6271\u3046 Json.Net DeserializeObject\u7de8\u30fbC#\u3067 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,20],"tags":[46,45],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-c","category-wpf","tag-dynamicjson","tag-json"],"_links":{"self":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=527"}],"version-history":[{"count":7,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":781,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/527\/revisions\/781"}],"wp:attachment":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}