我有一個C#列表看起來像這樣:C#以JSON序列化使用JSON.Net
var reqUsers = from user in users
select new
{
username = user.username,
firstName = user.firstName,
lastName = user.lastName,
email = user.email
};
我用下面的轉換/序列化JSON(Newtonsoft.JSON):
var json = JsonConvert.SerializeObject(reqUsers);
與上面的代碼我得到一個JSON字符串是這樣的:
[{ username: "alan", firstName: "Alan", lastName: "Johnson", email: "[email protected]" },
{ username: "allison", firstName: "Allison", lastName: "House", email: "[email protected]" },
{ username: "ryan", firstName: "Ryan", lastName: "Carson", email: "[email protected]" } ]
但這裏是我需要得到:因爲我用的把手templatin g -
var testdata = {
users: [
{ username: "alan", firstName: "Alan", lastName: "Johnson", email: "[email protected]" },
{ username: "allison", firstName: "Allison", lastName: "House", email: "[email protected]" },
{ username: "ryan", firstName: "Ryan", lastName: "Carson", email: "[email protected]" } ]
如何使用Serializer來命名JSON數組?如上所述?
工作就像一個魅力!謝謝你...... – 2013-04-05 21:10:12
當訪問JS中的joson時,我不得不使用'var thejson = @ Html.Raw(json);'沒有引號問題 – SeanKPS 2017-09-08 14:27:39