简体   繁体   English

反序列化格式化为字典的JSON字符串

[英]Deserializing a JSON string formatted as a dictionary

Using C# (.Net 4.6) and JSON.NET 使用C#(.Net 4.6)和JSON.NET

I'm currently trying to deserialize a large JSON string that has been presented in a format with multiple levels - I'm aiming to store some of this data in a flat DB table, going via a C# class to build the data in the required format that will be written back to each row. 我目前正在尝试反序列化以多种级别的格式呈现的大型JSON字符串-我的目标是将其中一些数据存储在平面DB表中,通过C#类在所需的数据库中构建数据将写回到每一行的格式。

Here's an example of the string format (made up data with line breaks added to increase readability): 这是字符串格式的示例(为增加可读性而组成的数据加上换行符):

{
    "Microsoft":
    {
        "name" : "Microsoft",
        "products" : ["Word", "Excel", ["TestThis","TestOrThis"]],
        "employees" : 
        [
            {"John" :{"name" :  "John","skills" : ["Support", "Programming"]}}, 
            {"Dave":{"name" :  "Dave", "skills" : ["Tester"]}}
        ]
    }
}

What I really want to end up with is a database row that has just some of this information, reading something like: 我最终想要得到的是一个数据库行,其中仅包含部分此类信息,内容如下:

"Company Name", "Employee Name" 

eg 例如

"Microsoft", "John"
"Microsoft", "Dave"
"IBM", "Ted"

Reading a basic JSON string is easy enough, however I'm new to using JSON which has left me stumped on how to break this down. 读取基本的JSON字符串很容易,但是使用JSON是我的新手,这让我很困惑如何进行分解。

We can deserialize your JSON by first defining a couple of classes like this: 我们可以通过首先定义几个这样的类来反序列化您的JSON:

class Company
{
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("employees")]
    public List<Dictionary<string, Employee>> Employees { get; set; }
}

class Employee
{
    [JsonProperty("name")]
    public string Name { get; set; }
}

Then we can deserialize into a Dictionary<string, Company> like this: 然后我们可以反序列化为Dictionary<string, Company>如下所示:

var companies = JsonConvert.DeserializeObject<Dictionary<string, Company>>(json);

You'll notice that wherever the keys can vary in the JSON (eg the company and employee names), we need to use a Dictionary in place of a static class. 您会注意到,无论键在JSON中有何变化(例如公司名称和员工名称),我们都需要使用Dictionary代替静态类。 Also, we can omit defining properties for the items we are not interested in, like products and skills. 此外,我们可以省略为我们不感兴趣的项目(例如产品和技能)定义属性的步骤。

Once we have the deserialized companies, we can loop through the results to arrive at the desired output like this: 一旦拥有反序列化的公司,我们就可以遍历结果以达到所需的输出,如下所示:

foreach(KeyValuePair<string, Company> kvp in companies)
{
    foreach (Dictionary<string, Employee> employees in kvp.Value.Employees)
    {
        foreach (KeyValuePair<string, Employee> kvp2 in employees)
        {
            Console.WriteLine(kvp.Value.Name + ", " + kvp2.Value.Name);
        }
    }
}

Output: 输出:

Microsoft, John
Microsoft, Dave

Fiddle: https://dotnetfiddle.net/FpK7AN 小提琴: https : //dotnetfiddle.net/FpK7AN

On site json2csharp.com generete classes from your JSON, your example isn't valid JSON. 在您的JSON站点json2csharp.com上的通用类上,您的示例不是有效的JSON。 And deserialize your JSON into generated classes. 并将您的JSON反序列化为生成的类。 Sometimes tool generates bad classes, so be carefull. 有时工具会生成错误的类,因此请当心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM