C# の JSON ファイルに書き込みます
この投稿では、C# で JavaScript Object Notation (JSON) ファイルに書き込む方法について説明します。
1.使用する JsonSerializer.Serialize()
方法
.NET 4.7.2 以降で JSON をシリアライズおよびデシリアライズするための推奨ソリューションは、 System.Text.Json
名前空間。を使用できます。 JsonSerializer
シリアライズ元およびデシリアライズ先のカスタム型を持つクラス。シリアライゼーションとデシリアライゼーションの場合、以下も含める必要があります System.Text.Json.Serialization
名前空間。
次のソリューションは、 JsonSerializer.Serialize() オブジェクトを JSON 文字列に変換するメソッド。次に、JSON 文字列をファイルに書き込みます。 File.WriteAllText()
方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System.IO; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); string json = JsonSerializer.Serialize(customers); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[{"Name":"Jason","Age":25},{"Name":"Nikki","Age":20}]
JSON 文字列をきれいに印刷するには、次のように設定できます。 JsonSerializerOptions.WriteIndented
に true
.ただし、JSON をフォーマットすると、パフォーマンスに悪影響を及ぼす可能性があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System.IO; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(customers, options); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[
{
"Name": "Jason",
"Age": 25
},
{
"Name": "Nikki",
"Age": 20
}
]
2.使用する JsonConvert.SerializeObject()
方法
または、 Json.NET
JSON にシリアライズおよびデシリアライズするためのライブラリ。の Newtonsoft.Json
名前空間は、 JsonConvert .NET 型と JSON 型の間で変換するためのいくつかのメソッドを提供するクラス。
次の例では、オブジェクトを JSON にシリアル化します。 SerializeObject() メソッドを使用して、JSON 文字列をファイルに出力します。 File.WriteAllText()
方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System.IO; using System.Collections.Generic; using Newtonsoft.Json; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); string json = JsonConvert.SerializeObject(customers.ToArray()); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[{"Name":"Jason","Age":25},{"Name":"Nikki","Age":20}]
読みやすくするために JSON をフォーマットするには、 Formatting.Indented
オプション JsonConvert.SerializeObject()
方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System.IO; using System.Collections.Generic; using Newtonsoft.Json; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); string json = JsonConvert.SerializeObject(customers.ToArray(), Formatting.Indented); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[
{
"Name": "Jason",
"Age": 25
},
{
"Name": "Nikki",
"Age": 20
}
]
3.使用する JavaScriptSerializer.Serialize()
方法
最後に、AJAX アプリケーションの場合は、 JavaScriptSerializer シリアライゼーションとデシリアライゼーションのためのクラス。オブジェクトを JSON 文字列にシリアライズするには、そのオブジェクトを使用できます。 Serialize()
方法。次の例で説明します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System.IO; using System.Collections.Generic; using System.Web.UI; using System.Web.Script.Serialization; public class Customer { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer {Name = "Jason", Age = 25}); customers.Add(new Customer {Name = "Nikki", Age = 20}); var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(customers); File.WriteAllText(@"C:\customers.json", json); } } |
Customers.json:
[{"Name":"Jason","Age":25},{"Name":"Nikki","Age":20}]
C# の JavaScript Object Notation (JSON) ファイルへの書き込みはこれですべてです。