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

79 Views Asked by At

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

3

There are 3 best solutions below

3
SMSTJ On BEST ANSWER

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.

3
Selaka Nanayakkara On

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 :

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Service
{
    public int Id { get; set; }
    public int CalendarId { get; set; }
    public string PublicId { get; set; }
}

public class ServiceGroup
{
    public string Id { get; set; }
    public List<string> Types { get; set; }
    public List<Service> Services { get; set; }
    public Names Names { get; set; }
}

public class Names
{
    [JsonProperty("en")]
    public string English { get; set; }
}

public class ApiResponse
{
    public string Name { get; set; }
    public string Value { get; set; }
    public List<ServiceGroup> ServicesGroups { get; set; } = new List<ServiceGroup> ();
}

class Program
{
    static void Main()
    {
        string jsonResponse = @"{
            ""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\""}}
            ]""
        }";

        ApiResponse apiResponse = JsonConvert.DeserializeObject<ApiResponse>(jsonResponse);

        Console.WriteLine($"Name: {apiResponse.Name}");
        Console.WriteLine($"Value: {apiResponse.Value}");

        foreach (var serviceGroup in apiResponse.ServicesGroups)
        {
            Console.WriteLine($"ServiceGroup ID: {serviceGroup.Id}, English Name: {serviceGroup.Names?.English}");
            foreach (var service in serviceGroup.Services)
            {
                Console.WriteLine($"  Service ID: {service.Id}, Calendar ID: {service.CalendarId}, Public ID: {service.PublicId}");
            }
        }
    }
}

Working .Netfiddle here

0
Serge On

you have to fix a json you published ( add "}" before the last "]") , after this I could use this code

var jObj=JObject.Parse(json);

var result = new RootObject {
name = (string) jObj["name"],
value = JsonConvert.DeserializeObject<List<ServicesGroup>>((string)jObj["value"])
};

or if you need just value

List<ServicesGroup> value = JsonConvert.DeserializeObject<List<ServicesGroup>>(
                                            (string)JObject.Parse(json)["value"]);