2012-06-14 45 views
3

我從PHP的編碼與德PHP utf8_encode.If我打印直接在文本顯示爲控制檯的AJAX功能的全文如下:jQuery.parseJSON u0092字符不會被解析

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

當我使用jquery.parseJSON並再次將文本打印到描述中時,文本解析如下:

Els cursos tenen lobjectiu daprofundir en lactitudperiodísticadels alumnes。

所有其他的unicode字符都被很好的解析,但是爲什麼沒有被解析?我做錯了什麼?

在此先感謝!

+0

你的原始字符串是什麼?試圖刪除utf8_encode? – OptimusCrime

+0

只是一句話:FF14'JSON.parse'正確解析你的字符串,包括'\ u0092'。 – Sirko

+1

如果我romve uf8_encode返回null與json_encode –

回答

2

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); 
    } 
})(); 

Live copy | source

輸出:

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

正如你所看到的,他們看起來是一樣的有和沒有控制字符,但我們可以從控制字符包含字符串中長度看。

+0

感謝您的答案,我能做些什麼來顯示它?我希望看到: Els cursos tenen l'objectiu d'aprofundir –

+1

@SergiMínguez:'str = str.replace(/ \ u0092/g,「'」)'should do it:[Live example](http:/ /jsbin.com/igocof/2)| [來源](http://jsbin.com/igocof/2/edit) –

+0

那顯而易見!謝謝大家! –

0

它被jQuery解析。一個簡單的測試可以告訴你:

> $.parseJSON('"\\u0092"').length 
1 
> $.parseJSON('"\\u0092"').charCodeAt(0) 
146 
> $.parseJSON('"\\u0092"').charCodeAt(0).toString(16) 
"92" 

它只是將無法顯示,請參閱@TJCrowders回答說。