C# 4.0 如何使用jsonconvert.serializeobject生成Json?
我需要使用Class和C# 4.0 如何使用jsonconvert.serializeobject生成Json?,c#-4.0,json.net,C# 4.0,Json.net,我需要使用Class和C.中的对象生成json如下 如果我需要添加json值 { "people": { "categoryName": "People", "results": [ { "title": "Joseph Sagayam", "url": "#person-Joseph", "description": "DBA", "actions": [ {
C.
中的对象生成json
如下
如果我需要添加json值
{
"people": {
"categoryName": "People",
"results": [
{
"title": "Joseph Sagayam",
"url": "#person-Joseph",
"description": "DBA",
"actions": [
{
"icon": "fa-user-plus",
"url": "#add-contact"
},
{
"icon": "fa-comment-o",
"url": "#message-contact"
},
{
"icon": "fa-birthday-cake",
"url": "#birthday"
}
]
}
]
}
}
该如何处理此代码
请告诉我怎么做。您需要的一切都应该在这里
请尝试以下代码:
类别:
public class Action
{
public string icon { get; set; }
public string url { get; set; }
}
public class YourModelClass
{
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public List<Action> actions { get; set; }
}
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new { myRoot = collection };
var output = JsonConvert.SerializeObject(collectionWrapper);
var collection = new List<YourModelClass>();
List<Action> newAction = new List<Action>();
newAction.Add(new Action() { icon = "fa-user-plus", url = "#add-contact" });
newAction.Add(new Action() { icon = "fa-comment-o", url = "#message-contact" });
newAction.Add(new Action() { icon = "fa-birthday-cake", url = "#birthday" });
dynamic collectionWrapper = new
{
myRoot = new YourModelClass()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = newAction.ToList()
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new
{
myRoot = new YourModelClass()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
public class Action
{
public string icon { get; set; }
public string url { get; set; }
}
public class Results
{
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public List<Action> actions { get; set; }
}
public class YourModelClass
{
public string categoryName { get; set; }
public List<Results> results { get; set; }
}
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new
{
people = new YourModelClass()
{
categoryName = "People",
results = new List<Results>
{
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
},
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
},
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
}
}
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
上次更新:
public class Action
{
public string icon { get; set; }
public string url { get; set; }
}
public class YourModelClass
{
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public List<Action> actions { get; set; }
}
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new { myRoot = collection };
var output = JsonConvert.SerializeObject(collectionWrapper);
var collection = new List<YourModelClass>();
List<Action> newAction = new List<Action>();
newAction.Add(new Action() { icon = "fa-user-plus", url = "#add-contact" });
newAction.Add(new Action() { icon = "fa-comment-o", url = "#message-contact" });
newAction.Add(new Action() { icon = "fa-birthday-cake", url = "#birthday" });
dynamic collectionWrapper = new
{
myRoot = new YourModelClass()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = newAction.ToList()
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new
{
myRoot = new YourModelClass()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
public class Action
{
public string icon { get; set; }
public string url { get; set; }
}
public class Results
{
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public List<Action> actions { get; set; }
}
public class YourModelClass
{
public string categoryName { get; set; }
public List<Results> results { get; set; }
}
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new
{
people = new YourModelClass()
{
categoryName = "People",
results = new List<Results>
{
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
},
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
},
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
}
}
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
类别:
public class Action
{
public string icon { get; set; }
public string url { get; set; }
}
public class YourModelClass
{
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public List<Action> actions { get; set; }
}
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new { myRoot = collection };
var output = JsonConvert.SerializeObject(collectionWrapper);
var collection = new List<YourModelClass>();
List<Action> newAction = new List<Action>();
newAction.Add(new Action() { icon = "fa-user-plus", url = "#add-contact" });
newAction.Add(new Action() { icon = "fa-comment-o", url = "#message-contact" });
newAction.Add(new Action() { icon = "fa-birthday-cake", url = "#birthday" });
dynamic collectionWrapper = new
{
myRoot = new YourModelClass()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = newAction.ToList()
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new
{
myRoot = new YourModelClass()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
public class Action
{
public string icon { get; set; }
public string url { get; set; }
}
public class Results
{
public string title { get; set; }
public string url { get; set; }
public string description { get; set; }
public List<Action> actions { get; set; }
}
public class YourModelClass
{
public string categoryName { get; set; }
public List<Results> results { get; set; }
}
var collection = new List<YourModelClass>();
dynamic collectionWrapper = new
{
people = new YourModelClass()
{
categoryName = "People",
results = new List<Results>
{
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
},
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
},
new Results()
{
title = "Joseph Sagayam",
url = "#person-Joseph",
description = "DBA",
actions = new List<Action>
{
new Action() { icon = "fa-user-plus", url = "#add-contact" },
new Action() { icon = "fa-comment-o", url = "#message-contact" },
new Action() { icon = "fa-birthday-cake", url = "#birthday" }
}
}
}
}
};
var output = JsonConvert.SerializeObject(collectionWrapper);
希望这对您有所帮助。谢谢您的回复。你能告诉我如何添加值吗?这真的很有帮助,然后请给出答案。如果我需要添加一些更像{“人”:{“categoryName”:“人”,“结果”:[{“title”:“Joseph Sagayam”,“url”:“person Joseph”,“description”:“DBA”,“actions”:[{“icon”:“fa user plus”,“url”:“add contact”},{“icon”:“fa-comment-o”,“url”:“#message contact”},{“icon”:“fa生日蛋糕”,“url”:“#birth”}]}]}}我必须做哪些更改?您能告诉我如何做这些更改吗?您的意思是在json输出字符串中添加这些值吗?