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

JSONの使い方を整理する。

参考

https://dev.classmethod.jp/etc/c-sharp-json/ by 加藤潤さん @ Developers.IO

情報感謝です。

上記掲載内容のうち、Newtonsoft.JSON (元Json.NET?)を使ってみる。

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)
        {
            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}");
        }
    }

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

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

qiita.png

link

Json.NET Documentation: Serializing and Deserializing JSON

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?