Json 在 C# 中用 Json.net 格式化
Json formatting with Json.net in C#
我正在尝试将一堆 XML 文件解析为单个 JSON 文件,这已经在工作了。
最终的 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"
},
...
相反,我想实现以下结构:
{
"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"
}
]
]
}
但我不知道如何定义我的对象才能这样做,我目前的结构如下:
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; }
}
我应该怎么做?
答案很简单:将您的对象变成列表:这将删除道具名称(和 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; }
}
正如@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; }
}
复制你的 json 然后进入 Visual Studio.
单击 "Edit" > "Paste Special" > "Paste JSON as Classes"
所有 类 都是自动创建的。希望这篇攻略能帮到你。
我正在尝试将一堆 XML 文件解析为单个 JSON 文件,这已经在工作了。
最终的 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"
},
...
相反,我想实现以下结构:
{
"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"
}
]
]
}
但我不知道如何定义我的对象才能这样做,我目前的结构如下:
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; }
}
我应该怎么做?
答案很简单:将您的对象变成列表:这将删除道具名称(和 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; }
}
正如@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; }
}
复制你的 json 然后进入 Visual Studio.
单击 "Edit" > "Paste Special" > "Paste JSON as Classes"
所有 类 都是自动创建的。希望这篇攻略能帮到你。