将 dotnet 对象序列化为 json as j属性 其中键是对象的 属性 并为整个对象赋值
serialize dotnet object to json as jproperty where key is a property of the object and value the whole object
我需要创建这个 json,我知道我可以使用字符串操作,但我想使用 类:
{
"articles":{
"3":{
"id":"3",
"label":"Preambolo",
"leaf":true,
"pathArray":[
{
"id":"1",
"label":"TITOLO I Disposizioni Generali"
},
{
"id":"2",
"label":"Capo I Definizioni e classificazioni generali"
}
],
"path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"
}
}
}
这是我的代码:
public class PathArrayItem {
public string id {
get;
set;
}
public string label {
get;
set;
}
public PathArrayItem(string id, string label) {
this.id = id;
this.label = label;
}
}
public class item {
public string id {
get;
set;
}
public string label {
get;
set;
}
public bool leaf {
get;
set;
}
public List < PathArrayItem > pathArray {
get;
set;
}
public string path {
get;
set;
}
}
void test() {
List < item > art = new List < item > ();
item item_3 = new item();
item_3.id = "3";
item_3.label = "Preambolo";
item_3.leaf = true;
item_3.pathArray = new List < PathArrayItem > ();
item_3.pathArray.Add(new PathArrayItem("1", "TITOLO I Disposizioni Generali"));
item_3.pathArray.Add(new PathArrayItem("2", "Capo I Definizioni e classificazioni generali"));
item_3.path = "TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali";
art.Add(item_3);
txtOutput.Text = Newtonsoft.Json.JsonConvert.SerializeObject(new {
articles = art
});
}
这是我的代码产生的输出:
{
"articles":[
{
"id":"3",
"label":"Preambolo",
"leaf":true,
"pathArray":[
{
"id":"1",
"label":"TITOLO I Disposizioni Generali"
},
{
"id":"2",
"label":"Capo I Definizioni e classificazioni generali"
}
],
"path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"
}
]
}
这是我需要改变的:
- 文章不能是数组。
- 每个项目都需要序列化为 jproperty,其中 id 是键,整个对象是值。
您可以使用 Dictionary<int, item>
而不是 List<item>
Dictionary<int, item> art = new Dictionary<int, item>();
item item_3 = new item();
item_3.id = "3";
item_3.label = "Preambolo";
item_3.leaf = true;
item_3.pathArray = new List<PathArrayItem>();
item_3.pathArray.Add(new PathArrayItem("1", "TITOLO I Disposizioni Generali"));
item_3.pathArray.Add(new PathArrayItem("2", "Capo I Definizioni e classificazioni generali"));
item_3.path = "TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali";
art.Add(3, item_3);
哪个会给你
{"articles":{"3":{"id":"3","label":"Preambolo","leaf":true,"pathArray":[{"id":"1","label":"TITOLO I Disposizioni Generali"},{"id":"2","label":"Capo I Definizioni e classificazioni generali"}],"path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"}}}
我需要创建这个 json,我知道我可以使用字符串操作,但我想使用 类:
{
"articles":{
"3":{
"id":"3",
"label":"Preambolo",
"leaf":true,
"pathArray":[
{
"id":"1",
"label":"TITOLO I Disposizioni Generali"
},
{
"id":"2",
"label":"Capo I Definizioni e classificazioni generali"
}
],
"path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"
}
}
}
这是我的代码:
public class PathArrayItem {
public string id {
get;
set;
}
public string label {
get;
set;
}
public PathArrayItem(string id, string label) {
this.id = id;
this.label = label;
}
}
public class item {
public string id {
get;
set;
}
public string label {
get;
set;
}
public bool leaf {
get;
set;
}
public List < PathArrayItem > pathArray {
get;
set;
}
public string path {
get;
set;
}
}
void test() {
List < item > art = new List < item > ();
item item_3 = new item();
item_3.id = "3";
item_3.label = "Preambolo";
item_3.leaf = true;
item_3.pathArray = new List < PathArrayItem > ();
item_3.pathArray.Add(new PathArrayItem("1", "TITOLO I Disposizioni Generali"));
item_3.pathArray.Add(new PathArrayItem("2", "Capo I Definizioni e classificazioni generali"));
item_3.path = "TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali";
art.Add(item_3);
txtOutput.Text = Newtonsoft.Json.JsonConvert.SerializeObject(new {
articles = art
});
}
这是我的代码产生的输出:
{
"articles":[
{
"id":"3",
"label":"Preambolo",
"leaf":true,
"pathArray":[
{
"id":"1",
"label":"TITOLO I Disposizioni Generali"
},
{
"id":"2",
"label":"Capo I Definizioni e classificazioni generali"
}
],
"path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"
}
]
}
这是我需要改变的:
- 文章不能是数组。
- 每个项目都需要序列化为 jproperty,其中 id 是键,整个对象是值。
您可以使用 Dictionary<int, item>
List<item>
Dictionary<int, item> art = new Dictionary<int, item>();
item item_3 = new item();
item_3.id = "3";
item_3.label = "Preambolo";
item_3.leaf = true;
item_3.pathArray = new List<PathArrayItem>();
item_3.pathArray.Add(new PathArrayItem("1", "TITOLO I Disposizioni Generali"));
item_3.pathArray.Add(new PathArrayItem("2", "Capo I Definizioni e classificazioni generali"));
item_3.path = "TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali";
art.Add(3, item_3);
哪个会给你
{"articles":{"3":{"id":"3","label":"Preambolo","leaf":true,"pathArray":[{"id":"1","label":"TITOLO I Disposizioni Generali"},{"id":"2","label":"Capo I Definizioni e classificazioni generali"}],"path":"TITOLO I Disposizioni Generali::Capo I Definizioni e classificazioni generali"}}}