2011-08-12 41 views
1

服務器返回數據:EXT Js中的Json數據讀取問題?

{ 
    "items": [{ 
     "id": "671", 
     "post_title": "Seche Vite Dry Fast Top Coat", 
     "post_content": "<span style=\"color\: \#ff0000\;\"><strong>Roy Recommends:<\/strong>\u00c2\u00a0 Edith, our in-house nail expert, swears by this product.\u00c2\u00a0 No manicure or pedicure is complete without adding this top coat as the finishing touch.<\/span>\r\n\r\nSeche Vite\u00e2\u201e\u00a2 dry fast top coat is widely acknowledged as the world\'s finest top coat.\u00c2\u00a0 Specially formulated to penetrate through nail lacquer to the base coat forming a single solid coating over the nail plate for a much more durable finish. Guaranteed not to yellow while leaving nails silky, stronger and resistant to chipping and peeling.\r\n\r\n&nbsp;", 
     "post_excerpt": "", 
     "post_status": "publish", 
     "post_parent": "0", 
     "parent_sort_id": "671", 
     "prod_meta_data_key": "_wpsc_product_metadata", 
     "category": "Roy Recommends,Nail Care", 
     "_wpsc_price": "10", 
     "_wpsc_special_price": "0", 
     "_wpsc_sku": "", 
     "_wpsc_stock": "4", 
     "unpublish_when_none_left": "1", 
     "weight": 8.5, 
     "weight_unit": "ounce", 
     "height": "0", 
     "height_unit": "in", 
     "width": "0  ", 
     "width_unit": "in", 
     "length": "0", 
     "length_unit": "in", 
     "local": "0", 
     "international": "0", 
     "no_shipping": "0" 
    }], 
    "totalCount": "1" 
} 

蔭得到FOLL錯誤:

Uncaught SyntaxError: Unexpected token ILLEGAL 
doDecodeext-all.js:7 
(anonymous function)ext-all.js:7 
o.callbacksm.js:249 
Ext.extend.handleResponseext-all.js:7 
fext-base.js:7 
mext-base.js:7 
(anonymous function) 

我發現了一個在線工具:http://json.parser.online.fr/

在這當我粘貼數據EVAL不能評估跨度&風格之間的空間,因爲eval只能執行表達式而不是語句。這裏突破是一個說法:

<span style=\"color\: \#ff0000\;\"> 

當我做preg_replace("/[ ]+/",'&nbsp;',$data)它的工作原理。現在問題是我不想讓 顯示,因爲我們允許用戶編輯文本&我希望它只顯示爲空格。

+1

哪個方法用於讀取上面的json字符串? – diEcho

回答

1

使用JSONLint我看到您的JSON無效。當它們不屬於合法的JSON轉義序列(\", \\, \/, \b, \f, \n, \r, \t, \u<4-digit-hex>)時,您必須轉義反斜槓(\)。

在這種情況下,post_content的值是非法的,因爲它具有像\:\#這樣的序列。它更改爲以下應該可以解決這個問題(當然,你必須解決您的服務器端代碼,以正確轉義JSON響應)

{ 
    "items": [ 
     { 
      "id": "671", 
      "post_title": "Seche Vite Dry Fast Top Coat", 
      "post_content": "<span style=\"color\\: \\#ff0000\\;\"><strong>Roy Recommends:</strong> Edith, our in-house nail expert, swears by this product. No manicure or pedicure is complete without adding this top coat as the finishing touch.</span>\u000d\u000a\u000d\u000aSeche Vite™ dry fast top coat is widely acknowledged as the world\\'s finest top coat. Specially formulated to penetrate through nail lacquer to the base coat forming a single solid coating over the nail plate for a much more durable finish. Guaranteed not to yellow while leaving nails silky, stronger and resistant to chipping and peeling.\u000d\u000a\u000d\u000a&nbsp;", 
      "post_excerpt": "", 
      "post_status": "publish", 
      "post_parent": "0", 
      "parent_sort_id": "671", 
      "prod_meta_data_key": "_wpsc_product_metadata", 
      "category": "Roy Recommends,Nail Care", 
      "_wpsc_price": "10", 
      "_wpsc_special_price": "0", 
      "_wpsc_sku": "", 
      "_wpsc_stock": "4", 
      "unpublish_when_none_left": "1", 
      "weight": 8.5, 
      "weight_unit": "ounce", 
      "height": "0", 
      "height_unit": "in", 
      "width": "0  ", 
      "width_unit": "in", 
      "length": "0", 
      "length_unit": "in", 
      "local": "0", 
      "international": "0", 
      "no_shipping": "0" 
     } 
    ], 
    "totalCount": "1" 
} 
+0

幫我逃過這個:它不能用反斜槓或雙反斜線工作 –

+0

你不需要手工轉義JSON字符串,你可能不會覆蓋所有的情況和你最終會輸出無效的JSON響應。這個JSON如何生成? –

+0

至於這個特定的情況,只需要轉義雙引號:'「」'。因此,你的JSON應該如下所示:'{「key」:「」}' –

0

我想它在Chrome中。只有在JSON字符串中的以下序列不能被Javascript中的eval()分析。其他人都很好。

\" \\ \n \r