C# Net反序列化字符串中的Json字符串

C# Net反序列化字符串中的Json字符串,c#,json,string,json.net,C#,Json,String,Json.net,这听起来很简单。但我不知道怎么做 我从api收到了一个错误的json。实际的json在字符串中 而不是 [{\"ProductId\":1,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":2,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":3,\"ProductName\":\"abuka\

这听起来很简单。但我不知道怎么做

我从api收到了一个错误的json。实际的json在字符串中

而不是

[{\"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}]
我收到了

"[{\"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}]"
当我尝试

JsonConvert.DeserializeObject<List<Product>> (jsonString)
JsonConvert.DeserializeObject(jsonString)
我在转换为System.Collections.Generic.List时出错


如何在反序列化之前将其提取到有效的JSON字符串中?

如果您得到的值是“序列化为JSON的字符串”,则只需首先反序列化该值即可。假设字符串真正以双引号开始和结束,则可以调用
JsonConvert.DeserializeObject
进行展开:

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}");
    }
}
使用系统;
使用System.IO;
使用Newtonsoft.Json;
公共类模型
{
公共字符串Foo{get;set;}
}
公开课考试
{
静态void Main()
{
字符串json=“\”{\\\\“foo\\\”:\\\\“bar\\\”}\”;
WriteLine($“原始JSON:{JSON}”);
字符串unwrappedJson=JsonConvert.DeserializeObject(json);
WriteLine($“Unwrapped JSON:{unwrappedJson}”);
Model Model=JsonConvert.DeserializeObject(unwrappedJson);
WriteLine($“model.Foo:{model.Foo}”);
}
}

如果您得到的值是“序列化为JSON的字符串”,那么只需首先反序列化该值。假设字符串真正以双引号开始和结束,则可以调用
JsonConvert.DeserializeObject
进行展开:

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}");
    }
}
使用系统;
使用System.IO;
使用Newtonsoft.Json;
公共类模型
{
公共字符串Foo{get;set;}
}
公开课考试
{
静态void Main()
{
字符串json=“\”{\\\\“foo\\\”:\\\\“bar\\\”}\”;
WriteLine($“原始JSON:{JSON}”);
字符串unwrappedJson=JsonConvert.DeserializeObject(json);
WriteLine($“Unwrapped JSON:{unwrappedJson}”);
Model Model=JsonConvert.DeserializeObject(unwrappedJson);
WriteLine($“model.Foo:{model.Foo}”);
}
}

谢谢Jon Skeet。参考您的答案,在应用程序中不使用“Newtonsoft.Json”但提供数据的Web API使用“Newtonsoft.Json”的用户可能会有所帮助

例如:

在WEB API中,使用了“Newtonsoft.Json”

使用WEB API的应用程序(可能是MVC/ASP.NET WebForms)不使用“Newtonsoft.Json”。因此,他们可以引用“
System.Web.Extensions
”并使用
using System.Web.Script.Serialization
来解析json数据

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

}
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
命名空间DesrializeJson1ConsoleApp
{
班级计划
{
静态void Main(字符串[]参数)
{
目前,在该地区的一名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名“;
var serializer=新的JavaScriptSerializer();
var jsonObject=serializer.Deserialize(jsonstring);
List lstProducts=serializer.Deserialize(jsonObject);
foreach(LST产品中的var项目)
{
Console.WriteLine(“ProductId:+item.ProductId”);
Console.WriteLine(“产品名称:+item.ProductName”);
Console.WriteLine(“费率:+项目费率”);
Console.WriteLine(“数量:+item.Quantity”);
Console.WriteLine(“-----------------------------------”);
}
Console.Read();
}
}
公共类产品
{
public int ProductId{get;set;}
公共字符串ProductName{get;set;}
公共双速率{get;set;}
公共双数量{get;set;}
}
}
输出


谢谢Jon Skeet。参考您的答案,在应用程序中不使用“Newtonsoft.Json”但提供数据的Web API使用“Newtonsoft.Json”的用户可能会有所帮助

例如:

在WEB API中,使用了“Newtonsoft.Json”

使用WEB API的应用程序(可能是MVC/ASP.NET WebForms)不使用“Newtonsoft.Json”。因此,他们可以引用“
System.Web.Extensions
”并使用
using System.Web.Script.Serialization
来解析json数据

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

}
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
命名空间DesrializeJson1ConsoleApp
{
班级计划
{
静态void Main(字符串[]参数)
{
目前,在该地区的一名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名“;
var serializer=新的JavaScriptSerializer();
var jsonObject=serializer.Deserialize(jsonstring);
List lstProducts=serializer.Deserialize(jsonObject);
foreach(LST产品中的var项目)
{
Console.WriteLine(“ProductId:+item.ProductId”);
Console.WriteLine(“产品名称:+item.ProductName”);
Console.WriteLine(“费率:+项目费率”);
Console.WriteLine(“数量:+item.Quantity”);
Console.WriteLine(“-----------------------------------”);
}
Console.Read();
}
}
公共类产品
{
public int ProductId{get;set;}
公共字符串ProductName{get;set;}
公共双速率{get;set;}
公共双数量{get;set;}
}
}
输出

反斜杠是否明确