Json.Net PopulateObject Appending list rather than setting value

.net, c#, json, json.net

Solution

You should tell Json.Net to replace the arrays, like this:

var serializerSettings = new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Replace};


JsonConvert.PopulateObject(jasonString, myObject, serializerSettings)

Problem

I am using Json.Net for .Net 4.5 and when using populate object on the following object it increments the List's with the content of the json rather than setting its value. Json.Net ``` JsonConvert.PopulateObject(string, object) ``` Class ``` class MySettingSubClass { public List<string> MyStringList1 = new List<string>(){"one", "two", "three"} } class MySetting { public string MyString = "MyString"; public int MyInt = 5; public MySettingSubClass MyClassObject = new MySettingSubClass(); public List<string> MyStringList2 = new List<string>{"one", "two", "three"}; } ``` When they initially load, all is correct, however reloading from JSON both MyStringLists are duplicated `"one", "two", "three", "one", "two", "three"`

Original source