简体   繁体   English

使用JavaScriptSerializer将序列化为JSON的麻烦

[英]Trouble Serializing To JSON Using JavaScriptSerializer

I'm having some trouble serializing an object to a JSON string using System.Web.Script.Serialization.JavaScriptSerializer. 我在使用System.Web.Script.Serialization.JavaScriptSerializer将对象序列化为JSON字符串时遇到了一些麻烦。 Whenever I try to do it, my strings are automatically html encoded. 每当我尝试这样做时,我的字符串都会自动进行html编码。 Is there a way to prevent this from happening? 有没有办法防止这种情况发生? I'd really like to avoid using an external library if possible (code is for .NET 4). 如果可能,我真的想避免使用外部库(代码适用于.NET 4)。 Here's my code: 这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        string myHtml = "<div class=\"blueBackground\">This is a really cool div:)</div>";
        int someOtherValue = 5;

        var jsonSerializer = new JavaScriptSerializer();

        string jsonObj = jsonSerializer.Serialize(new MyClass
        {
            StringProperty = myHtml,
            IntProperty = someOtherValue
        });

        Console.WriteLine(jsonObj);
        Console.ReadLine();
    }

    class MyClass
    {
        public string StringProperty { get; set; }
        public int IntProperty { get; set; }
    }
}

It outputs the string 它输出字符串

{"StringProperty":"\

This is a really cool div:)\","IntProperty":5} {“StringProperty”:“\\ u003cdiv class = \\”blueBackground \\“\\ u003e这是一个非常酷的div:)\\ u003c / div \\ u003e”,“IntProperty”:5}

Thanks! 谢谢!

Your strings are not HTML encoded. 您的字符串不是HTML编码的。 They are javascript encoded. 它们是javascript编码的。 JSON is intended to be read by javascript interpreters and your output is perfectly valid javascript as seen in this live demo . JSON旨在由javascript解释器读取,您的输出是完全有效的javascript,如本现场演示中所示 It's valid JSON and any standard JSON deserializer will be able to understand this output and deserialize it back to the original string. 它是有效的JSON,任何标准的JSON反序列化器都能够理解这个输出并将其反序列化回原始字符串。 So nothing to worry about. 所以没什么值得担心的。

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

相关问题 JSON 序列化时如何使用 JavaScriptSerializer 设置格式? - How to set formatting with JavaScriptSerializer when JSON serializing? JSON.parse没有解析Json字符串。 使用JavaScriptSerializer进行序列化 - JSON.parse not parsing Json string. Serializing with JavaScriptSerializer 使用JavaScriptSerializer将实体映射到JSON - Map entity to JSON using JavaScriptSerializer 使用JavaScriptSerializer解析异常的Json - Parsing unusual Json using JavaScriptSerializer 正确使用JavaScriptSerializer进行序列化 - Serializing with JavaScriptSerializer properly JSON使用C#JavaScriptSerializer进行对象 - JSON to object using C# JavaScriptSerializer 反序列化 JSON Object 在 C# 使用 ZBFC573B6803E33524DF568B204F5 或 JavaScript - Deserialize JSON Object in C# using JSON.NET or JavaScriptSerializer 无法将JSON反序列化为字典 <string, List<string> &gt;使用JavaScriptSerializer - Unable to deserialize JSON to a Dictionary<string, List<string>> using JavaScriptSerializer Dictionary中的对象是什么 <string, object> 什么时候使用JavaScriptSerializer反序列化Json? - What is the object in Dictionary<string, object> when Deserializing Json using JavaScriptSerializer? 使用System.Web.Script.Serialization.JavascriptSerializer反序列化JSON - 如何? - Using System.Web.Script.Serialization.JavascriptSerializer to deserialize JSON - how to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM