Json JavaScriptSerializer C# 中的序列化
Json serialization in JavaScriptSerializer C#
我在 C# 中的应用程序中有对象,我需要将其序列化为 JSON,但我需要自定义结果 json 如下所示:
My class :
public class OrderStatus
{
public string title { get; set; }
public int statusNo { get; set; }
public string description { get; set; }
public string message { get; set; }
public string icon { get; set; }
}
我需要将其转换为
{
"1": {
"icon": "orange_warning",
"title": "Pending Processing",
"message":
"We are processing your payment on our end which could take up to 30 minutes",
"description":
"Feel free to continue shopping while we process your payment. You can check the status of your order in Order History at any time."
},
"2": {
"icon": "done_success",
"title": "Order Successfully Placed",
"message": "",
"description":
"We have sent an email to your email address confirming your order."
}
}
json中显示的数字是我class中的StatusNo 我用这个方法序列化了class
new JavaScriptSerializer().Serialize(model)
您要从中转换的结构 class 不和谐。
转换的好方法是改变你的结构。
为此,您可以使用 Json2csharp.com。将您的 json 结构和过去复制到其中。
多亏了我的智力,我猜你有一个 OrderStatus
的 list 并且你得到的 json 的关键是statusNo
属性 你的 C# class.
要获得 JSON 的特定结构,您应该始终创建与结构匹配的 special classes 并在 class 之间进行转换es 并在最后使用默认的 JSON 序列化过程。在您的情况下,它可能看起来像这样:
public static class Program
{
static void Main()
{
var orders = new List<OrderStatus>
{
new OrderStatus
{
title = "Pending Processing",
statusNo = 1,
description = "Feel free to continue shopping while we process your payment. You can check the status of your order in Order History at any time.",
message = "We are processing your payment on our end which could take up to 30 minutes",
icon= "orange_warning",
},
new OrderStatus
{
title = "Order Successfully Placed",
statusNo = 2,
description = "We have sent an email to your email address confirming your order.",
message = "",
icon= "done_success",
},
};
var jsonStructure = orders
.ToDictionary(order => order.statusNo, order => new OrderStatusJson { icon = order.icon, title = order.title, message = order.message, description = order.description });
var json = JsonSerializer.Serialize(jsonStructure, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);
Console.ReadLine();
}
}
public class OrderStatusJson
{
public string icon { get; set; }
public string title { get; set; }
public string message { get; set; }
public string description { get; set; }
}
public class OrderStatus
{
public string title { get; set; }
public int statusNo { get; set; }
public string description { get; set; }
public string message { get; set; }
public string icon { get; set; }
}
其他一些相关提示:
- 在 C# 端,属性 名称使用 PascalCase。两种常用的 JSON 序列化程序(Newtonsoft 和 Microsoft)都可以配置为对双方使用正确的大小写。
- 如果需要一堆不同类型之间的映射,你应该看看 AutoMapper。
我在 C# 中的应用程序中有对象,我需要将其序列化为 JSON,但我需要自定义结果 json 如下所示:
My class :
public class OrderStatus
{
public string title { get; set; }
public int statusNo { get; set; }
public string description { get; set; }
public string message { get; set; }
public string icon { get; set; }
}
我需要将其转换为
{
"1": {
"icon": "orange_warning",
"title": "Pending Processing",
"message":
"We are processing your payment on our end which could take up to 30 minutes",
"description":
"Feel free to continue shopping while we process your payment. You can check the status of your order in Order History at any time."
},
"2": {
"icon": "done_success",
"title": "Order Successfully Placed",
"message": "",
"description":
"We have sent an email to your email address confirming your order."
}
}
json中显示的数字是我class中的StatusNo 我用这个方法序列化了class
new JavaScriptSerializer().Serialize(model)
您要从中转换的结构 class 不和谐。 转换的好方法是改变你的结构。 为此,您可以使用 Json2csharp.com。将您的 json 结构和过去复制到其中。
多亏了我的智力,我猜你有一个 OrderStatus
的 list 并且你得到的 json 的关键是statusNo
属性 你的 C# class.
要获得 JSON 的特定结构,您应该始终创建与结构匹配的 special classes 并在 class 之间进行转换es 并在最后使用默认的 JSON 序列化过程。在您的情况下,它可能看起来像这样:
public static class Program
{
static void Main()
{
var orders = new List<OrderStatus>
{
new OrderStatus
{
title = "Pending Processing",
statusNo = 1,
description = "Feel free to continue shopping while we process your payment. You can check the status of your order in Order History at any time.",
message = "We are processing your payment on our end which could take up to 30 minutes",
icon= "orange_warning",
},
new OrderStatus
{
title = "Order Successfully Placed",
statusNo = 2,
description = "We have sent an email to your email address confirming your order.",
message = "",
icon= "done_success",
},
};
var jsonStructure = orders
.ToDictionary(order => order.statusNo, order => new OrderStatusJson { icon = order.icon, title = order.title, message = order.message, description = order.description });
var json = JsonSerializer.Serialize(jsonStructure, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);
Console.ReadLine();
}
}
public class OrderStatusJson
{
public string icon { get; set; }
public string title { get; set; }
public string message { get; set; }
public string description { get; set; }
}
public class OrderStatus
{
public string title { get; set; }
public int statusNo { get; set; }
public string description { get; set; }
public string message { get; set; }
public string icon { get; set; }
}
其他一些相关提示:
- 在 C# 端,属性 名称使用 PascalCase。两种常用的 JSON 序列化程序(Newtonsoft 和 Microsoft)都可以配置为对双方使用正确的大小写。
- 如果需要一堆不同类型之间的映射,你应该看看 AutoMapper。