Skip to content

Instantly share code, notes, and snippets.

@GtTmy
Created June 27, 2018 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GtTmy/b1edf7669347d3cd20ec022f64f6fa91 to your computer and use it in GitHub Desktop.
Save GtTmy/b1edf7669347d3cd20ec022f64f6fa91 to your computer and use it in GitHub Desktop.
Json.Netでのデシリアライズの確認
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace JsonDemo
{
class Program
{
public static void Main(string[] arg)
{
var x = new Test();
x.Items.Clear();
x.Items.Add(6);
var json = JsonConvert.SerializeObject(x);
var y = JsonConvert.DeserializeObject<Test>(json);
foreach (var i in y.Items)
{
System.Diagnostics.Debug.Write(i.ToString() + ",");
// -> 1,2,3,4,5,6,
}
System.Diagnostics.Debug.WriteLine("");
var settings = new JsonSerializerSettings
{
ObjectCreationHandling = ObjectCreationHandling.Replace,
};
var z = JsonConvert.DeserializeObject<Test>(json, settings);
foreach (var i in z.Items)
{
System.Diagnostics.Debug.Write(i.ToString() + ",");
// -> 6,
}
System.Diagnostics.Debug.WriteLine("");
}
public class Test
{
public IList<int> Items { get; set; } = new List<int> { 1, 2, 3, 4, 5 };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment