0
1

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 1 year has passed since last update.

Mono: C# で Json.NETの使い方

Last updated at Posted at 2022-08-08

こちらのサンプルを動くプログラムにしました。
Json.NET

シリアライズ

serialize01.cs
// --------------------------------------------------------------------
/*
	serialize01.cs

						Aug/08/2022
*/
// --------------------------------------------------------------------
using	System;
using	System.IO;

using Newtonsoft.Json;
// --------------------------------------------------------------------
namespace JsonSample
{
class Product
    {
        public string Name { get; set; }
        public DateTime Expiry { get; set; }
        public string[] Sizes { get; set; }
    }



class	json_read
{
// --------------------------------------------------------------------
static	void	Main (string[] args)
{
	Console.WriteLine ("*** 開始 ***");

	Product product = new Product();
	product.Name = "Apple";
	product.Expiry = new DateTime(2008, 12, 28);
	product.Sizes = new string[] { "Small","Large" };

string str_json = JsonConvert.SerializeObject(product);

	Console.WriteLine (str_json);

	Console.WriteLine ("*** 終了 ***");
}

// --------------------------------------------------------------------
}

// --------------------------------------------------------------------
}
// --------------------------------------------------------------------
Makefile
serialize01.exe: serialize01.cs
	mcs serialize01.cs -r:Newtonsoft.Json.dll
clean:
	rm -f *.exe

実行

$ ./serialize01.exe 
*** 開始 ***
{"Name":"Apple","Expiry":"2008-12-28T00:00:00","Sizes":["Small","Large"]}
*** 終了 ***

デシリアライズ

deserialize01.cs
// --------------------------------------------------------------------
/*
	deserialize01.cs

						Aug/08/2022
*/
// --------------------------------------------------------------------
using	System;
using	System.IO;

using Newtonsoft.Json;
// --------------------------------------------------------------------
namespace JsonSample
{
class Movie
    {
        public string Name { get; set; }

        public DateTime ReleaseDate { get; set; }

        public string[] Genres { get; set; }
    }



class	json_read
{
// --------------------------------------------------------------------
static	void	Main (string[] args)
{
	Console.WriteLine ("*** 開始 ***");

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";


Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;

Console.WriteLine(name);
Console.WriteLine(m.ReleaseDate);

foreach (string genre in m.Genres)
	{
	Console.WriteLine(genre);
	}

	Console.WriteLine ("*** 終了 ***");
}

// --------------------------------------------------------------------
}

// --------------------------------------------------------------------
}
// --------------------------------------------------------------------
Makefile
deserialize01.exe: deserialize01.cs
	mcs deserialize01.cs -r:Newtonsoft.Json.dll
clean:
	rm -f *.exe

実行結果

$ ./deserialize01.exe 
*** 開始 ***
Bad Boys
1995/04/07 0:00:00
Action
Comedy
*** 終了 ***

Newtonsoft.Json.dll

Json130r1.zip をダウンロードして解凍します。

Bin/net45/Newtonsoft.Json.dll を使います。
シンボリックリンクを張って使えます。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?