How to Deserialize a string which contains multiple object of same type in appended format [C#]

29 views Asked by At

Let's say I have an Employee class.

Employee
{
   string Name;
   int Age
}

My string contains the data of different company's employee in a string. I want deserialize it as List of Company Info. I have Company Class. Is there any solution which can deserialize multiple appended objects represented as string as following .

[{"companyName":"ABC","Employees":[{"Name":"X","Age":24},{"Name":"Y","Age":27}]}]
[{"companyName":"XYZ","Employees":[{"Name":"A","Age":24},{"Name":"B","Age":27}]}]

I want to know If there is an existing solution for it or I need to write my own JSON reader.

1

There are 1 answers

0
Avrohom Yisroel On

Given the sample you have shown, it is pretty easy to convert to valid Json. You need to replace the ] at the end of the first (and subsequent, but not including the last) lines with a comma, and remove the [ at the beginning of all lines other than the first, eg...

[{"companyName":"ABC","Employees":[{"Name":"X","Age":24},{"Name":"Y","Age":27}]},
{"companyName":"XYZ","Employees":[{"Name":"A","Age":24},{"Name":"B","Age":27}]}]

That is then valid Json that can be parsed by any standard parser.