-2

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.

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • What you've shown isn't valid Json. If it were, you could just use the built-in .NET deserialiser. If that's really the pseudo-Json you've been given, you can either fix it to be valid, or split it down into individual Json chunks and deserialise each one in turn. – Avrohom Yisroel Oct 28 '22 at 15:43
  • yes, It's not a valid json. I'm looking if there is some existing solution which can parse it and deserialize it as valid json object. – monalisha saha Oct 28 '22 at 16:55

1 Answers1

0

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.

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106