{"id":674,"date":"2020-03-25T17:11:45","date_gmt":"2020-03-25T08:11:45","guid":{"rendered":"https:\/\/todosoft.net\/blog\/?p=674"},"modified":"2021-03-14T05:39:18","modified_gmt":"2021-03-13T20:39:18","slug":"post-674","status":"publish","type":"post","link":"https:\/\/todosoft.net\/blog\/?p=674","title":{"rendered":"C#\u3067Json\u3092\u6271\u3046 Json.Net JToken\/JObject\/JArray\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\">Json.Net\u306b\u3064\u3044\u3066<\/h2>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\"Json.NET - Newtonsoft (\u65b0\u3057\u3044\u30bf\u30d6\u3067\u958b\u304f)\" href=\"https:\/\/www.newtonsoft.com\/json\" target=\"_blank\">Json.NET &#8211; Newtonsoft<\/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=\"WpfApp7.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:WpfApp7\"\n             StartupUri=\"View\\MainView.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=\"WpfApp7.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:WpfApp7.View\"\n        xmlns:vm=\"clr-namespace:WpfApp7.ViewModel\"\n        mc:Ignorable=\"d\"\n        Title=\"MainView\" Height=\"450\" Width=\"800\">\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;Grid>\n        &lt;Grid.RowDefinitions>\n            &lt;RowDefinition Height=\"auto\"\/>\n            &lt;RowDefinition Height=\"*\"\/>\n            &lt;RowDefinition Height=\"auto\"\/>\n        &lt;\/Grid.RowDefinitions>\n\n        &lt;Label Grid.Row=\"0\" Grid.Column=\"0\" Content=\"{Binding Title}\"\/>\n\n        &lt;ListView Grid.Row=\"1\" Grid.Column=\"0\" ItemsSource=\"{Binding arrayForecasts}\">\n            &lt;ListView.Style>\n                &lt;Style TargetType=\"ListView\">\n                    &lt;Setter Property=\"ScrollViewer.HorizontalScrollBarVisibility\" Value=\"Visible\"\/>\n                    &lt;Setter Property=\"ScrollViewer.VerticalScrollBarVisibility\" Value=\"Hidden\"\/>\n                &lt;\/Style>\n            &lt;\/ListView.Style>\n            &lt;ListView.ItemsPanel>\n                &lt;ItemsPanelTemplate>\n                    &lt;VirtualizingStackPanel Orientation=\"Horizontal\"\/>\n                &lt;\/ItemsPanelTemplate>\n            &lt;\/ListView.ItemsPanel>\n            &lt;ListView.ItemTemplate>\n                &lt;DataTemplate>\n                    &lt;StackPanel>\n                        &lt;Label Content=\"{Binding Date}\"\/>\n                        &lt;Image Source=\"{Binding ImageURL}\"\/>\n                        &lt;Label Content=\"{Binding Telop}\"\/>\n                    &lt;\/StackPanel>\n                &lt;\/DataTemplate>\n            &lt;\/ListView.ItemTemplate>\n        &lt;\/ListView>\n\n        &lt;Label Grid.Row=\"2\" Grid.Column=\"0\" Content=\"{Binding Description}\"\/>\n    &lt;\/Grid>\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 Newtonsoft.Json;\nusing Newtonsoft.Json.Linq;\nusing System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Linq;\nusing System.Text;\nusing System.Text.RegularExpressions;\nusing System.Threading.Tasks;\n\nnamespace WpfApp7.ViewModel\n{\n    public class MainViewModel : Livet.ViewModel\n    {\n        public class clsForecasts\n        {\n            public string Date { get; set; } = \"\";\n            public string ImageURL { get; set; } = \"\";\n            public string Telop { get; set; } = \"\";\n        }\n\n        private string _Title = \"\";\n        public string Title\n        {\n            get\n            {\n                return _Title;\n            }\n            set\n            {\n                _Title = value;\n                RaisePropertyChanged();\n            }\n        }\n\n        private string _Description = \"\";\n        public string Description\n        {\n            get\n            {\n                return _Description;\n            }\n            set\n            {\n                _Description = value;\n                RaisePropertyChanged();\n            }\n        }\n\n        private ObservableCollection&lt;clsForecasts> _arrayForecasts = new ObservableCollection&lt;clsForecasts>();\n        public ObservableCollection&lt;clsForecasts> arrayForecasts\n        {\n            get\n            {\n                return _arrayForecasts;\n            }\n            set\n            {\n                _arrayForecasts = 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            \/\/ JToken\u306b\u5909\u63db \u4e0b\u8a183\u3064\u306e\u624b\u6cd5\u3069\u308c\u3067\u3082\u3088\u3044\n            JToken objJToken = JToken.Parse(strSource);\n            \/\/JToken objJToken = JsonConvert.DeserializeObject&lt;JToken>(strSource);\n            \/\/JToken objJToken = (JToken)JsonConvert.DeserializeObject(strSource);\n\n            \/\/ \u5024\u306e\u53d6\u308a\u51fa\u3057\u65b9\u2460 JToken\u304b\u3089\u3044\u304d\u306a\u308a\n            Title = objJToken[\"title\"].ToString();\n\n            \/\/ \u5024\u306e\u53d6\u308a\u51fa\u3057\u65b9\u2461 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3042\u308bJObject\u306b\u5909\u63db\u3057\u3066\u304b\u3089\u53d6\u5f97\n            Description = (objJToken is JObject) ? ((JObject)objJToken)[\"description\"][\"text\"].ToString() : \"\";\n\n            \/\/ \u914d\u5217\u304b\u5426\u304b\u306f\u3001JArray\u578b\u304b\u5426\u304b\u3067\u5224\u5b9a\n            if (((JObject)objJToken)[\"forecasts\"] is JArray)\n            {\n                foreach(JObject objForecast in ((JObject)objJToken)[\"forecasts\"])\n                {\n                    arrayForecasts.Add(\n                        new clsForecasts() {\n                            Date = objForecast[\"date\"].ToString(),\n                            ImageURL = objForecast[\"image\"][\"url\"].ToString(),\n                            Telop = objForecast[\"telop\"].ToString()\n                        }\n                    );\n                }\n            }\n\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2020\/03\/json.net_.png\" alt=\"\" class=\"wp-image-679\" srcset=\"https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2020\/03\/json.net_.png 786w, https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2020\/03\/json.net_-300x169.png 300w, https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2020\/03\/json.net_-768x433.png 768w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/figure>\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,15],"tags":[45,83],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry","category-c","category-wpf","category-15","tag-json","tag-json-net"],"_links":{"self":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/674","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=674"}],"version-history":[{"count":6,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":687,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/674\/revisions\/687"}],"wp:attachment":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}