0

I'm using NewtonSoft for json serialization of below-mentioned class

class SampleTest
    {
        public ArrayList tmpList = new ArrayList(3) { new Sample12 { a=2, b=3},
            new Sample12 { a=45, b=5}, new Sample12 { a=2, b=3} };
    }

class Sample12
{
    public int a;
    public int b;
}

Using below code for serialization and deserialization

var obj = new SampleTest();
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
SampleTest deserializedobj = Newtonsoft.Json.JsonConvert.DeserializeObject<SampleTest>(jsonString);

Below is the json I'm getting.It's a valid JSON.

{ "tmpList":[
{
"a":2, "b":3 }, {
"a":45, "b":5 }, {
"a":2, "b":3 } ] }

While deserializing the JSON, I'm getting 6 records instead of 3 records. Out of that 3 are the valid object and remaining 3 are invalid JSON (JSON enclosed in curly braces)

enter image description here

Below one is one of the invalid JSON,

{{ "a": 2, "b": 3 }}

Why the last 3 records are added up while deserializing and How can I get rid of those 3 records? Is there any way to deserialize the last 3 records?

thejustv
  • 2,009
  • 26
  • 42
  • i guess it is because the library creates the object and inserts the values to the attributes. Therefore it uses the constructor. After initialization the static field tmpList will initialized again with new List and items. – Sunchezz May 28 '19 at 15:25
  • You are correct, I added JsonSerializerSettings to avoid duplicates. But only the last 3 records are coming and it's not a valid JSON. – thejustv May 29 '19 at 06:31

0 Answers0