Json.net反序列化的列表给出了重复项 [英] Json.net deserializing list gives duplicate items
问题描述
我刚开始使用Newtonsoft.Json(Json.net)。在我的第一个简单的测试,我反序列化泛型列表时遇到了一个问题。在我的代码示例如下序列化对象,包含三种类型的简单的整数列表(财产,成员var和数组)的。
I have just started using Newtonsoft.Json (Json.net). In my first simple test, I ran into a problem when deserializing generic lists. In my code sample below I serialize an object, containing three types of simple integer lists (property, member var and array).
生成的JSON看起来很好(的名单转换成JSON阵列)。然而,当我反序列化JSON回同一类型的一个新对象,所有列表项被复制,期望的阵列。我已经说明,通过序列化它第二次。
The resulting json looks fine (the lists are converted into json-arrays). However, when I deserialize the json back to a new object of the same type, all list items are duplicated, expect for the array. I've illustrated that by serializing it a second time.
大约从搜索,我读过,有可能是一个私人支持字段的列出了。解串器也就罢了
From searching around, I've read that there may be a "private" backing field to the lists that the deserializer also fills.
所以我的问题是:是否有一个(最好是简单的)方式,以避免在下列情况下,重复项目
So my question is: Is there a (preferably simple) way to avoid duplicate items in following case?
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace JsonSerializeExample
{
public class Program
{
static void Main()
{
var data = new SomeData();
var json = JsonConvert.SerializeObject(data);
Console.WriteLine("First : {0}", json);
var data2 = JsonConvert.DeserializeObject<SomeData>(json);
var json2 = JsonConvert.SerializeObject(data2);
Console.WriteLine("Second: {0}", json2);
}
}
public class SomeData
{
public string SimpleField;
public int[] IntArray;
public IList<int> IntListProperty { get; set; }
public IList<int> IntListMember;
public SomeData()
{
SimpleField = "Some data";
IntArray = new[] { 7, 8, 9 };
IntListProperty = new List<int> { 1, 2, 3 };
IntListMember = new List<int> { 4, 5, 6 };
}
}
}
结果输出
Resulting output
First : {"SimpleField":"Some data","IntArray":[7,8,9],"IntListMember":[4,5,6],"IntListProperty":[1,2,3]}
Second: {"SimpleField":"Some data","IntArray":[7,8,9],"IntListMember":[4,5,6,4,5,6],"IntListProperty":[1,2,3,1,2,3]}
有可能会出现一些与的 Json.Net复制私人列表项。不过,我觉得我的问题就更简单了,而我还没有想通了。
There may be some overlap here with Json.Net duplicates private list items. However, I think my problem is even simpler, and I still haven't figured it out.
推荐答案
这是因为你添加在构造函数项。在解串器处理列表时,通常的做法基本上是:
That is because you are adding items in the constructor. A common approach in deserializers when processing a list is basically:
- 通过吸气
读取列表- 如果该列表为空:创建一个新的列表,并通过属性setter分配,如果一个
这是因为大多数列表成员没有setter方法,即
this is because most list members don't have setters, i.e.
public List<Foo> Items {get {...}} // <=== no set
与数组不同,它必须有一个设置器,是有用的;因此,这种方法通常是:
Contrast to arrays, which must have a setter to be useful; hence the approach is usually:
- 反序列化反过来每个项目,追加(
添加
)到一个临时目录 - 列表转换为数组(
的ToArray
),并通过二传分配
- deserialize each item in turn, and append (
Add
) to a temporary list - convert the list to an array (
ToArray
), and assign via the setter
有些串行给你选项来控制这种行为(别人不一样);有的串行给你完全绕过构造(别人不一样)的能力。
Some serializers give you options to control this behavior (others don't); and some serializers give you the ability to bypass the constructor completely (others don't).
这篇关于Json.net反序列化的列表给出了重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!