简体   繁体   English

使用JavaScript解析.NET编码的JSON时出现意外字符

[英]Unexpected Character when Parsing .NET-Encoded JSON with JavaScript

I'm developing an ASP.NET Web Pages app, and I'm seeing a problem where the Javascript JSON.parse() method is unable to parse JSON output by the .NET Json.Encode() method. 我正在开发一个ASP.NET网页应用程序,并且遇到一个Javascript JSON.parse()方法无法解析.NET Json.Encode()方法输出的JSON的问题。 My specific problem is with the ampersand (&) character (Unicode U+0026). 我的特定问题是与号(&)字符(Unicode U + 0026)。

For example, executing this code: 例如,执行以下代码:

object SomeObject = new { SomeProperty = "A&B" };
Response.Write(Json.Encode(SomeObject));

In my .cshtml file results in the following content in the response: 在我的.cshtml文件中,响应中包含以下内容:

{"SomeProperty":"A\u0026B"}

Which leads to a SyntaxError: Unexpected token u in my JavaScript: 这导致我的JavaScript中出现SyntaxError: Unexpected token u

function SomeCallback(aRequest) {
    if (aRequest.status === 200) {
        var lResponseJSON = JSON.parse(aRequest.Response); // Error on this line
    }
}

How can I get the .NET JSON encoding and the JS JSON decoding to play nice when special characters are involved? 当涉及特殊字符时,如何才能使.NET JSON编码和JS JSON解码发挥出色?

(Preferably, short of manually going through the stringified JSON before it's parsed to replace the unicode encodings) (最好是,在解析替换JSON编码之前,最好不要手动处理字符串化的JSON)

EDIT: Might be worth mentioning that using Json.Write(SomeObject, Response.Output) instead of Response.Write(Json.Encode(SomeObject)) has no effect on the JSON output. 编辑:可能值得一提,使用Json.Write(SomeObject, Response.Output)而不是Response.Write(Json.Encode(SomeObject))对JSON输出没有影响。

Your problem has to be somewhat different that you are showing: 您显示的问题必须有所不同:

When I run this code through my console: 当我通过控制台运行以下代码时:

var k = JSON.parse('{"SomeProperty":"A\u0026B"}')
console.log(k);
// Object {SomeProperty: "A&B"}

everything behaves correctly. 一切正常。

Though it looks strange, this is valid JSON: 虽然看起来很奇怪,但这是有效的JSON:

{"SomeProperty":"A\u0026B"}

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

相关问题 解析 JSON.net 上的值时遇到意外字符 - Unexpected character encountered while parsing value on JSON net json.net,JsonReaderException:解析值后,遇到意外字符 - json.net, JsonReaderException: After parsing a value an unexpected character was encountered JSon解析值时遇到意外字符:[ - JSon unexpected character encountered while parsing value: [ Android-JSON解析会引发意外的字符异常 - Android - JSON Parsing throws unexpected character exception Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符 - ASP.NET - Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value - ASP.NET C#Json.net解析值时遇到意外字符:[ - C# Json.net Unexpected character encountered while parsing value: [ 反序列化JSON对象时发生错误“解析值时遇到意外字符:&lt;。 路径”,第0行,位置0。” - Error when deserializing JSON object “Unexpected character encountered while parsing value: <. Path '', line 0, position 0.” 解析值时遇到意外字符:S.反序列化JSON时的路径 - Unexpected character encountered while parsing value: S. Path when Deserializing JSON 解析Json.NET:“意外的令牌:StartObject” - Parsing with Json.NET: “Unexpected token: StartObject” 使用Json.NET解析:意外的令牌:StartObject - Parsing with Json.NET: Unexpected token: StartObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM