[英]Json serialization in JavaScriptSerializer C#
I have object in my app in c# i need to serialize it to JSON but I need to custom the result json to be like below :我的应用程序中有对象在 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; }
}
I need to convert it to我需要将其转换为
{
"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."
}
}
the number is shown in json is StatusNo in my class I use this method to serialize the class json 中显示的数字是我的类中的 StatusNo 我使用此方法来序列化类
new JavaScriptSerializer().Serialize(model)
Thanks to my mental powers, I would guess, that you have a list of OrderStatus
and that the key in your resulting json is the statusNo
property of your C# class.由于我的智力,我猜你有一个OrderStatus
列表,并且你生成的 json 中的键是你的 C# 类的statusNo
属性。
To get a specific structure as JSON you should always create special classes that match the structure and convert between your classes and at the end use the default JSON serialization process.要获得特定的 JSON 结构,您应该始终创建与结构匹配的特殊类并在您的类之间进行转换,最后使用默认的 JSON 序列化过程。 In your case it could look something like this:在您的情况下,它可能看起来像这样:
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; }
}
Some other related tips:其他一些相关提示:
the structure you want to convert from that class is not have harmony.您要从该类转换的结构不和谐。 the good way to convert that is change your structure.转换它的好方法是改变你的结构。 for that you can use Json2csharp.com .为此,您可以使用Json2csharp.com 。 copy your json structure and past into that.复制您的 json 结构并过去。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.