0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio | WPF > JSON > デシリアライズ > Newtonsoft.JSON使用 > List<T>型オブジェクト

Last updated at Posted at 2017-11-14
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

参考

処理概要

List<Person>型のデシリアライズを行う。

IDE

NugetにてNewtonsoft.JSON v10.0.3をプロジェクトにインストールした。

code

MainWindow.xaml
<Window x:Class="_171114_t1600_jsonWrite.MainWindow"
        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:local="clr-namespace:_171114_t1600_jsonWrite"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="B_json" Height="30" Content="JSON" Click="B_json_Click"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// 以下を追加
using Newtonsoft.Json;

namespace _171114_t1600_jsonWrite
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void B_json_Click(object sender, RoutedEventArgs e)
        {
            // 1.  Serialize
            var prsnlst = new List<Person>();
            prsnlst.Add(new Person{ Name="Janeway", Gender="Female"});
            prsnlst.Add(new Person { Name = "Tuvok", Gender = "Male" });
            prsnlst.Add(new Person { Name = "Chakotay", Gender = "Male" });

            var json = JsonConvert.SerializeObject(prsnlst);
            Console.WriteLine($"{json}");

            // 2. Deserialize
            Console.WriteLine("-----");
            var dstlst = JsonConvert.DeserializeObject<List<Person>>(json);
            foreach (var elem in dstlst)
            {
                Console.WriteLine($"{elem.Name} is {elem.Gender}");
            }
        }
    }

    public class Person
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("gender")]
        public string Gender { get; set; }
    }
}

qiita.png

補足

JsonConvert.DeserializeObject<List<T>>()が失敗する場合はIEnumerable<T>を使うと動く、という例がある。

(こちらの環境ではList<T>で失敗しなかった)。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?