2011-08-12 13 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" 
} 

Iamはfoll errを取得:私はデータのevalを貼り付けるときにできませんhttp://json.parser.online.fr/

れる:

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) 

を私はオンラインツールを見つけましたspan &スタイルの間のスペースを評価すると、evalは式not文だけを実行できます。ここで休憩は文です:

<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の値は不正です。以下にそれを変更すると、私はChromeでそれを試してみました

{ 
    "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は次のようになります: '' ":" "}' –

0

(もちろん、あなたが適切にJSONレスポンスを逃れるために、サーバー側のコードを修正する必要があります)問題を修正する必要があります。 JSON文字列の次のシーケンスのみをJavascriptのeval()で解析できません。他は大丈夫です。

\" \\ \n \r 
関連する問題