简体   繁体   English

JavaScriptSerializer()。Serialize:PascalCase到CamelCase

[英]JavaScriptSerializer().Serialize : PascalCase to CamelCase

I have this javascript object 我有这个javascript对象

var options:
 {
        windowTitle         : '....',
        windowContentUrl    : '....',
        windowHeight        : 380,
        windowWidth         : 480
}

And I have this C# class 我有这个C#类

public class JsonDialogViewModel
    {
        public string WindowTitle               {   get;    set;    }
        public string WindowContentUrl          {   get;    set;    }
        public double WindowHeight              {   get;    set;    }
        public double WindowWidth               {   get;    set;    }

    }

And you see, my notation is PascalCase in C# and my Javascript is CamelCase. 你看,我的符号是C#中的PascalCase,而我的Javascript是CamelCase。 That the usual convention. 通常的惯例。

I am using JavaScriptSerializer().Serialize to serialize my C# object and use it in my Javascript code. 我正在使用JavaScriptSerializer()。Serialize来序列化我的C#对象并在我的Javascript代码中使用它。

I am however facing this issue of PascalCase to CamelCase that JavaScriptSerializer().Serialize does not handle. 然而,我面临的问题是PascalCase到CamelCase,JavaScriptSerializer()。Serialize无法处理。

What do you suggest to get around this translation? 您有什么建议来解决这个翻译?

Thank you 谢谢

The best solution I could find was to have a method that receives the object to be serialized, generates a Dictionary<string, object> based on the properties of the object and then apply JavaScriptSerializer.Serialize() to this Dictionary. 我能找到的最佳解决方案是有一个方法接收要序列化的对象,根据Dictionary<string, object>的属性生成Dictionary<string, object> ,然后将JavaScriptSerializer.Serialize()应用于此Dictionary。
This was good enough for what I needed. 这足以满足我的需要。

Maybe you can use DataContractJsonSerializer instead? 也许您可以使用DataContractJsonSerializer代替? Here's a related questions with an answer that explains how to use it: ASP.NET MVC: Controlling serialization of property names with JsonResult 这是一个相关问题,答案解释了如何使用它: ASP.NET MVC:使用JsonResult控制属性名称的序列化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM