简体   繁体   English

javascript json-从php解码ajax json数组时出现问题

[英]javascript json - problem decoding ajax json array from php

I'm using php's json_encode() to convert an array to json which then echo's it and is read from a javascript ajax request. 我正在使用php的json_encode()将数组转换为json,然后将其回显并从javascript ajax请求中读取。

The problem is the echo'd text has unicode characters which the javascript json parse() function doesn't convert to. 问题是echo'd文本有unicode字符,javascript json parse()函数没有转换为。

Example array value is "2\0\1\0\-\1\0\-\0\1" which is "2010-10-01". 示例数组值为“2 \\ u00000 \\ u00000 \\ u0000 \\ u0000 \\ u00000 \\ u00000 \\ u0000 \\ u00000 \\ u00001”,即“2010-10-01”。

Json.parse() only gives me "2". Json.parse()只给我“2”。

Anyone help me with this issue? 有人帮我解决这个问题吗?

Example: 例:

            var resArray = JSON.parse(this.responseText);
            for(var x=0; x < resArray.length; x++) {
                var twt = resArray[x];
                alert(twt.date);
                break;
            }

You have NUL characters (character code zero) in the string. 字符串中包含NUL个字符(字符代码为零)。 It's actually "2_0_1_0_-_1_0_-_0_1" , where _ represents the NUL characters. 它实际上是"2_0_1_0_-_1_0_-_0_1" ,其中_代表NUL字符。

The unicode character escape is actually part of the JSON standard, so the parser should handle that correctly. unicode字符转义实际上是JSON标准的一部分,因此解析器应该正确处理它。 However, the result is still a string will NUL characters in it, so when you try to use the string in Javascript the behaviour will depend on what the browser does with the NUL characters. 但是,结果仍然是字符串中的NUL字符,因此当您尝试在Javascript中使用字符串时,行为将取决于浏览器对NUL字符的作用。

You can try this in some different browsers: 您可以在某些不同的浏览器中尝试此操作:

alert('as\u0000df');

Internet Explorer will display only as Internet Explorer将仅显示as

Firefox will display asdf but the NUL character doesn't display. Firefox将显示asdf但不显示NUL字符。

The best solution would be to remove the NUL characters before you convert the data to JSON. 最好的解决方案是在将数据转换为JSON之前删除NUL字符。

To add to what Guffa said: 加上古法所说的话:

When you have alternating zero bytes, what has almost certainly happened is that you've read a UTF-16 data source without converting it to an ASCII-compatible encoding such as UTF-8. 当你有零字节交替时,几乎可以肯定的是你已经读过一个UTF-16数据源而没有将它转换为UTF-8等ASCII兼容编码。 Whilst you can throw away the nulls, this will mangle the string if it contains any characters outside of ASCII range. 虽然您可以丢弃空值,但如果它包含ASCII范围之外的任何字符,则会破坏该字符串。 (Not an issue for date strings of course, but it may affect any other strings you're reading from the same source.) (日期字符串当然不是问题,但是它可能会影响您从同一来源读取的任何其他字符串。)

Check where your PHP code is reading the 2010-10-01 string from, and either convert it on the fly using eg iconv('utf-16le', 'utf-8', $string) , or change the source to use a more reasonable encoding. 检查您的PHP代码从哪里读取2010-10-01字符串,并使用例如iconv('utf-16le', 'utf-8', $string)转换它,或者更改源代码以使用更合理的编码。 If it's a text file, for example, save it in a text editor using 'UTF-8 without BOM', and not 'Unicode', which is a highly misleading name Windows text editors use to mean UTF-16LE. 例如,如果它是文本文件,则使用“UTF-8无BOM”保存在文本编辑器中,而不是“Unicode”,这是Windows文本编辑器用来表示UTF-16LE的极具误导性的名称。

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

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