jQuery.parseJSON \ u0092字符未解析 [英] jQuery.parseJSON \u0092 character is not parsed

查看:108
本文介绍了jQuery.parseJSON \ u0092字符未解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本是从php编码的,带有ajax函数和de php utf8_encode.如果我直接在控制台中将其打印出来,则文本显示如下:

"projects":[
    {
        "id": "1",
        "title": "CURSOS DE PERIODISME",
        "description": "Els cursos tenen l\u0092objectiu d\u0092aprofundir en l\u0092actitud period\u00edstica dels alumnes."
    }
]

当我使用jquery.parseJSON并将文本再次打印到描述中时,文本解析如下:

校友期报

对所有其他unicode字符都进行了很好的解析,但是为什么不解析\ u0092?我做错了什么?

提前谢谢!

解决方案

U + 0092 是控制字符,也许正在解析中,但是由于您使用字符串的方式而看不到它.

例如,此代码根本不进行JSON解析:

(function() {

  var strWith = "Els cursos tenen l\u0092objectiu d\u0092aprofundir";
  var strWithout = "Els cursos tenen lobjectiu daprofundir";

  display("With    (" + strWith.length + "): " + strWith);
  display("Without (" + strWithout.length + "): " + strWithout);

  function display(msg) {
    var p = document.createElement('pre');
    p.innerHTML = String(msg);
    document.body.appendChild(p);
  }
})();

实时复制 |

输出:

With    (40): Els cursos tenen lobjectiu daprofundir
Without (38): Els cursos tenen lobjectiu daprofundir

如您所见,带和不带控制字符的外观都相同,但是从长度上我们可以看到字符串中包含了控制字符 .

I have a text encoded from php with an ajax function with de php utf8_encode.If I print it in the console directly the text is displayed as follows :

"projects":[
    {
        "id": "1",
        "title": "CURSOS DE PERIODISME",
        "description": "Els cursos tenen l\u0092objectiu d\u0092aprofundir en l\u0092actitud period\u00edstica dels alumnes."
    }
]

When I use the jquery.parseJSON and print the text again into the description, the text is parsed as follows:

Els cursos tenen lobjectiu daprofundir en lactitud periodística dels alumnes.

All other unicode characters are well parsed, but why \u0092 is not parsed? What I'm doing wrong?

Thanks in advance!

解决方案

U+0092 is a control character, perhaps it's being parsed but you're not seeing it because of how you're using the string.

For example, this code which does no JSON parsing at all:

(function() {

  var strWith = "Els cursos tenen l\u0092objectiu d\u0092aprofundir";
  var strWithout = "Els cursos tenen lobjectiu daprofundir";

  display("With    (" + strWith.length + "): " + strWith);
  display("Without (" + strWithout.length + "): " + strWithout);

  function display(msg) {
    var p = document.createElement('pre');
    p.innerHTML = String(msg);
    document.body.appendChild(p);
  }
})();

Live copy | source

Output:

With    (40): Els cursos tenen lobjectiu daprofundir
Without (38): Els cursos tenen lobjectiu daprofundir

As you can see, they look the same with and without the control character, but we can see from the lengths that the control character is included in the string.

这篇关于jQuery.parseJSON \ u0092字符未解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆