我需要在using Class
和C#
中生成json
。
{
"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"
}
]
}
如果我需要添加blow 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);
输出:那么您将得到如下所示的输出
{
"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 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);
输出:那么您将得到如下所示的输出
{
"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"
}
]
},
{
"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"
}
]
},
{
"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"
}
]
}
]
}
}
你需要的一切都应该在这里http://www.newtonsoft.com/json
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
string json = JsonConvert.SerializeObject(product);
// {
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
// }