JsonResult 将特殊字符解析为 \u0027(撇号)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5021995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JsonResult parsing special chars as \u0027 (apostrophe)
提问by Richard
I am in the process of converting some of our web "services" to MVC3 from WCF Rest.
我正在将我们的一些网络“服务”从 WCF Rest 转换为 MVC3。
Our old web services returned JSON from POCO's just fine using:
[WebGet(.... ResponseFormat=WebMessageFormat.Json]
我们的旧 Web 服务使用以下方法从 POCO 返回 JSON 就好了:
[WebGet(.... ResponseFormat=WebMessageFormat.Json]
In my controller to return back a simple poco I'm using a JsonResult as the return type, and creating the json with Json(someObject, ...).
在我的控制器中返回一个简单的 poco,我使用 JsonResult 作为返回类型,并使用Json(someObject, ...).
In the WCF Rest service, the apostrophes and special chars are formatted cleanly when presented to the client.
在 WCF Rest 服务中,撇号和特殊字符在呈现给客户端时格式清晰。
In the MVC3 controller, the apostrophes appear as \u0027.
在 MVC3 控制器中,撇号显示为 \u0027。
Any thoughts? I'm new to serializing JSON so any pointers would be a huge help.
有什么想法吗?我是序列化 JSON 的新手,所以任何指针都会有很大的帮助。
Example response:
WCF Rest:
{"CategoryId":8,"SomeId":6,"Name":"Richie's House"}
示例响应:WCF 休息:
{"CategoryId":8,"SomeId":6,"Name":"Richie's House"}
MVC3:
{"CategoryId":8,"SomeId":6,"Name":"Richie\u0027s House"}
MVC3:
{"CategoryId":8,"SomeId":6,"Name":"Richie\u0027s House"}
回答by Darin Dimitrov
That shouldn't be any problem, as both representations are equivalent:
这应该没有任何问题,因为这两种表示是等效的:
var a = {"CategoryId":8,"SomeId":6,"Name":"Richie\u0027s House"};
alert(a.Name);
alerts Richie's House.
警报Richie's House。
回答by Alexandre TRINDADE
Just do:
做就是了:
yourObject.Name = yourObject.Name.Replace("'", "\u027");
So, if you try to alert in javascript or show in a browser, it will appears like:
因此,如果您尝试在 javascript 中发出警报或在浏览器中显示,它将显示为:
Richie's House
里奇的房子
回答by Krishna
U+0027is Unicode for apostrophe (')
U+0027是单引号 (') 的 Unicode
So, special characters are returned in Unicode but will show up properly when rendered on the page.
因此,特殊字符以 Unicode 返回,但在页面上呈现时会正确显示。

