2014-01-29 16 views
0

我正在使用某些Web服務生成動態JavaScript。獲取錯誤格式'"'而不是普通引號(「」)

我得到這個結果:

[{"values": [{"count": 1, "text": "Yes"}, {"count": 0, "text": "No"}], "key": "Welcome?"}]; 

我想要什麼:

[{"values": [{"count": 1, "text": "Yes"}, {"count": 0, "text": "No"}], "key": "Welcome?"}]; 

目前,我在.replace(/"/g,'"')

管理,但我想更妥善的解決方案,如果可能的。

感謝

+1

你如何檢索響應,什麼格式?看起來像嚴重解析JSON ... – elclanrs

回答

0

看起來你正在使用您的JSON html_entities。

我所做的是建立和數組,然後json_encode它。

<?php 
    $data = array(
     "values" => array(
      array("count" => 1, "text" => "Yes"), 
      array("count" => 0, "text" => "No"), 
     ), 
     "key" => "Welcome?" 
    ); 

    $json_data = json_encode($data); 

得到完全相同的JSON作爲你的問題只是包裝的數據陣列中的另一個數組:

<?php 
    $data = array(
     array(
      "values" => array(
       array("count" => 1, "text" => "Yes"), 
       array("count" => 0, "text" => "No"), 
      ), 
      "key" => "Welcome?" 
     ) 
    ); 

    $json_data = json_encode($data); 
+0

我使用簡單的JavaScript。 以及來自Web服務的數據。 –

+0

對不起,我假設PHP,因爲我一直咬引用的東西......也許看看:http://stackoverflow.com/questions/5957546/javascript-regex-replacing-quot – NiGhTHawK

相關問題