简体   繁体   English

在C#中使用Json.net进行Json格式化

[英]Json formatting with Json.net in C#

I am trying to parse a bunch of XML files into a single JSON file, which is already working. 我正在尝试将一堆XML文件解析为一个JSON文件,该文件已经可以工作了。

The final JSON file looks like the following: 最终的JSON文件如下所示:

{
"items": [
    {
        "subItems": [
            {
                "Name": "Name",
                "Value": "Value",
                "Type": "text"
            },
            {
                "Name": "Name",
                "Value": "Value",
                "Type": "text"
            }
        ]
    },
    {
        "subItems": [
            {
                "Name": "Name",
                "Value": "Value",
                "Type": "text"
            },
            {
                "Name": "Name",
                "Value": "Value",
                "Type": "text"
            },
...

Instead, I want to achieve the following structure: 相反,我想实现以下结构:

{
"items": [
    [   
        {
            "Name": "Name",
            "Value": "Value",
            "Type": "text"
        },
        {
            "Name": "Name",
            "Value": "Value",
            "Type": "text"
        }

    ],
    [
        {
            "Name": "Name",
            "Value": "Value",
            "Type": "text"
        },
        {
            "Name": "Name",
            "Value": "Value",
            "Type": "text"
        }
    ]
]
}

But I don't know how to define my objects in order to do so, my current structure is as follows: 但是我不知道如何定义我的对象,我目前的结构如下:

public class Items
{
    public List<Item> items;
}

public class Item
{
    public List<SubItem> subItems;
}

public class SubItem
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Type { get; set; }
}

How should I do this? 我应该怎么做?

The answer is simple: turn your objects into lists: This will remove the prop names (and object notation in json). 答案很简单:将您的对象转换为列表:这将删除属性名称(以及json中的对象表示法)。

public class Items
{
    public List<Item> items; //list with prop name 'items'
}

public class Item : List<SubItem> // list in list like json notation
{
}

public class SubItem // Object in the list in list
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Type { get; set; }
}

As @FlilipCordas noted Inheriting from list is bad practice (for good reason) you are better of this way: 正如@FlilipCordas指出的那样,从列表继承是一种不好的做法(有充分的理由),这样您会更好:

public class Items
{
    public List<List<SubItem>> items; //list with list with prop name 'items'
}

public class SubItem // Object in the list in list
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Type { get; set; }
}

Copy your json then go in Visual Studio. 复制您的json,然后进入Visual Studio。
Click on "Edit" > "Paste Special" > "Paste JSON as Classes" 单击“编辑”>“选择性粘贴”>“将JSON粘贴为类”

All the classes are automatically created. 所有的类都是自动创建的。 I hope this tip could help you. 希望本文对您有所帮助。

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

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