Search code examples
c#jsonreplacejson.netjson-deserialization

Unable to deserialize the API response json in .NET Core C#


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: enter image description here


      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; }
     }


Solution

  • 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

    //This is what your Json will Deserialize into/ what you will have to Serialize to get the same JSON fromat
    public class RootObjectWithValueString
    {
        public string name { get; set; }
        public string value { get; set; }
    
        public RootObject ToRootObject()
        {
            List<ServicesGroup> val = JsonConvert.DeserializeObject<List<ServicesGroup>>(value);
            RootObject robj = new RootObject(){name = name, value = val};
            return robj;
        }
    }
    
    //This contains the values in a manner for you to work with
    public class RootObject
    {
        public string name { get; set; }
        public List<ServicesGroup> value { get; set; }
    
        public RootObjectWithValueString ToROB()
        {
            string val = JsonConvert.SerializeObject(value);
            RootObjectWithValueString rob = new RootObjectWithValueString(){name = name, value = val};
            return rob;
        }
    }
    

    And this is how one would deserialize your JSON into your RootObject

    public void ResponseJsonToObject(string servicegroupsjson)
    {
        RootObjectWithValueString rob = JsonConvert.DeserializeObject<RootObjectWithValueString>(servicegroupsjson);
        RootObject robj = rob.ToRootObject();
    }
    

    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.