反序列化带有嵌入在字符串值中的子对象的JSON字符串 [英] Deserialize JSON string with child objects embedded in a string value

查看:104
本文介绍了反序列化带有嵌入在字符串值中的子对象的JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,想一举反序列化为C#.

I have a JSON String that I'm trying to deserialize to C# in one swoop.

SalesLines的子节点是字符串表示形式.反序列化时,我一直希望对象一直向下.使用JSON.NET的最佳方法是什么?

The Children nodes of SalesLines is a string representation. I want objects all the way down when I deserialize. What is the best way to this with JSON.NET?

{
   "value":[
      {
         "documentType":"Quote",
         "SONumber":"S-QUO1001",
         "SalesLines":"[{\"SONumber\":\"S-QUO1001\",\"LineNum\":10000,\"ItemId\":\"1936-S\",\"ItemAttributes\":[{\"AttibuteName\":\"COLOR\",\"AttributeValue\":\"YELLOW\"},{\"AttibuteName\":\"DEPTH\",\"AttributeValue\":\"100\"},{\"AttibuteName\":\"WIDTH\",\"AttributeValue\":\"120\"},{\"AttibuteName\":\"HEIGHT\",\"AttributeValue\":\"115\"},{\"AttibuteName\":\"MATERIAL DESCRIPTION\",\"AttributeValue\":\"COTTON, WOOD LEGS\"},{\"AttibuteName\":\"MODEL YEAR\",\"AttributeValue\":\"1940\"}]}]"
      }
   ]
}

推荐答案

SalesLines 属性的值是双序列化JSON:一个字符串值,其中包含嵌入为字符串文字的JSON.您想一步将其内容反序列化为最终的数据模型.

The value of your SalesLines property is double-serialized JSON: a string value that contains JSON embedded as a string literal. You would like to deserialize its contents to a final data model in one step.

要查看数据模型的外观,可以按如下所示对JSON进行转义:

To see what the data model should look like, you can unescape the JSON as follows:

var json = JToken.Parse(jsonString);

foreach(var token in json.SelectTokens("value[*].SalesLines").ToList())
{
    token.Replace(JToken.Parse((string)token));
}

Console.WriteLine(json);

然后使用 如何从JSON对象字符串自动生成C#类文件中提到的代码生成工具之一 从未转义的JSON生成数据模型(我使用了 http://json2csharp.com/):

public class ItemAttribute
{
    public string AttibuteName { get; set; }
    public string AttributeValue { get; set; }
}

public class SalesLine
{
    public string SONumber { get; set; }
    public int LineNum { get; set; }
    public string ItemId { get; set; }
    public List<ItemAttribute> ItemAttributes { get; set; }
}

public class Value
{
    public string documentType { get; set; }
    public string SONumber { get; set; }
    public List<SalesLine> SalesLines { get; set; }
}

public class RootObject
{
    public List<Value> value { get; set; }
}

最后,将 EmbeddedLiteralConverter< List< SalesLine>> 此答案应用于 如何将JSON对象中的转义JSON字符串转换为 Value 代码>:

Finally, apply EmbeddedLiteralConverter<List<SalesLine>> from this answer to How do I convert an escaped JSON string within a JSON object? to Value:

public class Value
{
    public string documentType { get; set; }
    public string SONumber { get; set; }

    [JsonConverter(typeof(EmbeddedLiteralConverter<List<SalesLine>>))]
    public List<SalesLine> SalesLines { get; set; }
}

现在您将能够将JSON直接反序列化为 RootObject :

Now you will be able to deserialize the JSON to RootObject directly:

root = JsonConvert.DeserializeObject<RootObject>(jsonString);

演示小提琴此处.

这篇关于反序列化带有嵌入在字符串值中的子对象的JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆