I have 3rd party web api that returns json response contains escape characters and many backslashes as below.
My aim to read all service group and its related services. I am unable to deseriable even after using replace
, regex
as well.
{
"name": "servicesGroups",
"value": "[{"id":"ServiceGroup1","types":["books"],"services":[{"id":2,"calendarId":2,"publicId":"1234567890"},{"id":1,"calendarId":1,"publicId":"0987654321"}],"names":{"en":"Service Group1"}},{"id":"ServiceGroup2","types":["books"],"services":[{"id":2,"calendarId":2,"publicId":"7575637576457457567"},{"id":1,"calendarId":1,"publicId":"2143658709"}],"names":{"en":"ServiceGroup2"}]"
}
That’s how I see through debug:
RootObject root = JsonConvert.DeserializeObject<RootObject>(servicegroupsjson);
List<ServicesGroup> servicesGroups = JsonConvert.DeserializeObject<List<ServicesGroup>>(root.value); - Compile time error
public class Service
{
public int id { get; set; }
public int calendarId { get; set; }
public string publicId { get; set; }
}
public class Names
{
public string en { get; set; }
}
public class ServicesGroup
{
public string id { get; set; }
public List<string> types { get; set; }
public List<Service> services { get; set; }
public Names names { get; set; }
}
public class RootObject
{
public string name { get; set; }
public List<ServicesGroup> value { get; set; }
}
2
Answers
You need to create an instance of List and assign it into value. then you can deserialize the object with Newtonsoft.Json.
Refer the code below :
Ok so ive tried to keep this as close to your source code as possible. As @grek40 already mentioned, when you do the initial deserilazation you need to make value a string type, since thats whats actually in your JSON. Then you can deserialize that value string into your list.
So this is the new class I added and the modification I made to your RootObject
And this is how one would deserialize your JSON into your RootObject
Now a thing I need to mention is the fact that the JSON you gave us seems faulty. At the end there is one too many }. In
"names":{"en":"ServiceGroup2"}}}]"
, you of course have one to close names, another to close the Service but then you should have the ] to close the list. Instead there is a } in the way. So I dont know if that is just a mistake that came in during your copying the json into stackoverflow, or if this is a mistake in the json itself. now if the latter is the case thats a big problem.