【问题标题】:Json serialization in JavaScriptSerializer C#JavaScriptSerializer C# 中的 Json 序列化
【发布时间】:2021-12-16 18:06:42
【问题描述】:

我在 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中显示的数字是StatusNo在我的类中我使用这个方法来序列化类

new JavaScriptSerializer().Serialize(model)

【问题讨论】:

  • 这个问题质量极低,请阅读How to Ask 并在以后添加回答它所需的适当信息

标签: c# asp.net .net serialization jsonconvert


【解决方案1】:

感谢我的精神力量,我猜你有一个OrderStatus列表,并且生成的 json 中的键是你的 C# 类的 statusNo 属性。 p>

要获得 JSON 格式的特定结构,您应该始终创建与结构匹配的 特殊类,并在您的类之间进行转换,最后使用默认的 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

【讨论】:

    【解决方案2】:

    您要从该类转换的结构不和谐。 转换的好方法是改变你的结构。 为此,您可以使用Json2csharp.com。将您的 json 结构复制到其中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      相关资源
      最近更新 更多