Drunken Daddy
Drunken Daddy

Reputation: 8001

Json.Net deserialize JSON string inside a string

This sound very simple. But I can't find how to do it.

I've receiving a bad json from an api. the actual json is inside a string

Instead of

[{\"ProductId\":1,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":2,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":3,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000}]

I'm receiving

"[{\"ProductId\":1,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":2,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":3,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000}]"

When I try

JsonConvert.DeserializeObject<List<Product>> (jsonString)

I get error Error converting to System.Collections.Generic.List

How can I extract it into a valid JSON string before deserialising?

Upvotes: 2

Views: 13158

Answers (2)

Chandan Kumar
Chandan Kumar

Reputation: 4638

Thanks Jon Skeet. Referring to your answer, the users who are not using "Newtonsoft.Json" in the application but the Web API which gives the data uses "Newtonsoft.Json" for them below one might be helpful.

Example :

In WEB API - "Newtonsoft.Json" used

The application(might be MVC/ASP.NET WebForms) which consumes WEB API, doesn't use "Newtonsoft.Json". So, they can reference to "System.Web.Extensions" and use using System.Web.Script.Serialization to desialize the json data.

using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace DesrializeJson1ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string jsonstring = "\"[{\\\"ProductId\\\":1,\\\"ProductName\\\":\\\"abuka\\\",\\\"Rate\\\":6.00,\\\"Quantity\\\":10.000},{\\\"ProductId\\\":2,\\\"ProductName\\\":\\\"abuka\\\",\\\"Rate\\\":6.00,\\\"Quantity\\\":10.000},{\\\"ProductId\\\":3,\\\"ProductName\\\":\\\"abuka\\\",\\\"Rate\\\":6.00,\\\"Quantity\\\":10.000}]\"";
            var serializer = new JavaScriptSerializer();
            var jsonObject = serializer.Deserialize<string>(jsonstring);
            List<Product> lstProducts = serializer.Deserialize<List<Product>>(jsonObject);
            foreach(var item in lstProducts)
            {
                Console.WriteLine("ProductId :" + item.ProductId);
                Console.WriteLine("ProductName :" + item.ProductName);
                Console.WriteLine("Rate :" + item.Rate);
                Console.WriteLine("Quantity :" + item.Quantity);
                Console.WriteLine("--------------------------");
            }
            Console.Read();
        }
    }
    public class Product
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public double Rate { get; set; }
        public double Quantity { get; set; }
    }

}

OUTPUT

enter image description here

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1504132

If you've got a value which is "a string serialized as JSON", then just deserialize that first. Assuming your string genuinely starts and ends with a double quote, you should be fine to call JsonConvert.DeserializeObject<string> to do that unwrapping:

using System;
using System.IO;
using Newtonsoft.Json;

public class Model
{
    public string Foo { get; set; }
}

public class Test
{
    static void Main()
    {
        string json = "\"{\\\"foo\\\": \\\"bar\\\"}\"";
        Console.WriteLine($"Original JSON: {json}");        
        string unwrappedJson = JsonConvert.DeserializeObject<string>(json);
        Console.WriteLine($"Unwrapped JSON: {unwrappedJson}");
        Model model = JsonConvert.DeserializeObject<Model>(unwrappedJson);
        Console.WriteLine($"model.Foo: {model.Foo}");
    }
}

Upvotes: 7

Related Questions