2015-05-21 46 views
-1

我在php腳本中有以下簡單的json。json字符串中的HTML導致錯誤

echo '{"rows":"'.$rows.'", "other_prop":{}}'; 

$rows是:

<tr><td>this is gant</td> 
     <td>2015-05-21 12:42:11</td> 
     <td><pre>[gant id="135"]</pre></td> 
     <td><a href="#" class="button load-gant" data-gantname="_GANT_this_is_gant__2015-05-21 12:42:11">Load</a> <a href="" class="button delete-gant red" data-gantname="_GANT_this_is_gant__2015-05-21 12:42:11">Delete</a></td></tr><tr><td>bunda</td> 
     <td>2015-05-21 12:47:36</td> 
     <td><pre>[gant id="139"]</pre></td> 
     <td><a href="#" class="button load-gant" data-gantname="_GANT_bunda__2015-05-21 12:47:36">Load</a> <a href="" class="button delete-gant red" data-gantname="_GANT_bunda__2015-05-21 12:47:36">Delete</a></td></tr> 

JSON皮棉一直說

Parse error on line 2: 
{ "rows": "<tr><td>this is gan 
-------------^ 
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[' 

我所沒有試圖逃跑每個雙引號,並且還使用htmlentities()逃脫每標籤。但皮棉仍然用同樣的話說。

最新的問題,以及如何解決這個問題?

+0

呃... ...'$ rows'不包含JSON。 –

+0

@YUNOWORK:已更新。行只是一個字符串作爲其值的屬性 –

+0

@DarielPratama我的猜測是,你的問題是你的'''沒有在html中轉義並且打破了JSON字符串。你能發佈你通過lint放入的整個JSON嗎?還是看到我的答案代碼,爲您處理這個 – EdgeCaseBerg

回答

1

工作細跟的$rows

<tr><td>thisisgant</td><td>2015-05-2112: 42: 11</td><td><pre>[gant id='135']</pre></td><td><a href='#' class='buttonload-gant' data-gantname='_GANT_this_is_gant__2015-05-2112: 42: 11'>Load</a> <a href='' class='buttondelete-gantred' data-gantname='_GANT_this_is_gant__2015-05-2112: 42: 11'>Delete</a></td></tr><tr><td>bunda</td> <td>2015-05-21 12:47:36</td><td><pre>[gant id='139']</pre></td><td><a href='#' class='button load-gant' data-gantname='_GANT_bunda__2015-05-21 12:47:36'>Load</a> <a href='' class='button delete-gant red' data-gantname='_GANT_bunda__2015-05-21 12:47:36'>Delete</a></td></tr> 

該值只要保持在其有效值在單行中並全部替換爲"'

0

爲什麼不使用json_encode和php對象?例如:

<?php 
$rows =<<<HTML 
<tr><td>this is gant</td> 
<td>2015-05-21 12:42:11</td> 
<td><pre>[gant id="135"]</pre></td> 
<td><a href="#" class="button load-gant" data-gantname="_GANT_this_is_gant__2015-05-21 12:42:11">Load</a> <a href="" class="button delete-gant red" data-gantname="_GANT_this_is_gant__2015-05-21 12:42:11">Delete</a></td></tr><tr><td>bunda</td> 
<td>2015-05-21 12:47:36</td> 
<td><pre>[gant id="139"]</pre></td> 
<td><a href="#" class="button load-gant" data-gantname="_GANT_bunda__2015-05-21 12:47:36">Load</a> <a href="" class="button delete-gant red" data-gantname="_GANT_bunda__2015-05-21 12:47:36">Delete</a></td></tr> 
HTML; 
$obj = new stdClass(); 
$obj->rows = $rows; 
echo json_encode($obj); 

這產生的代碼:

{"rows":"<tr><td>this is gant<\/td>\r  <td>2015-05-21 12:42:11<\/td>\r\t  <td><pre>[gant id=\"135\"]<\/pre><\/td>\r\t\t  <td><a href=\"#\" class=\"button load-gant\" data-gantname=\"_GANT_this_is_gant__2015-05-21 12:42:11\">Load<\/a> <a href=\"\" class=\"button delete-gant red\" data-gantname=\"_GANT_this_is_gant__2015-05-21 12:42:11\">Delete<\/a><\/td><\/tr><tr><td>bunda<\/td>\r\t\t\t  <td>2015-05-21 12:47:36<\/td>\r\t\t\t\t  <td><pre>[gant id=\"139\"]<\/pre><\/td>\r\t\t\t\t\t  <td><a href=\"#\" class=\"button load-gant\" data-gantname=\"_GANT_bunda__2015-05-21 12:47:36\">Load<\/a> <a href=\"\" class=\"button delete-gant red\" data-gantname=\"_GANT_bunda__2015-05-21 12:47:36\">Delete<\/a><\/td><\/tr>"} 

根據JSON棉絨

0

你要這樣定義你的變量使用它在JSON,這將是字符串..

$row="<tr><td>this is gant</td>"+ 
    "<td>2015-05-21 12:42:11</td>"+ 
    "<td><pre>[gant id='135']</pre></td>"+ 
    "<td><a href='#' class='button load-gant' data-gantname='_GANT_this_is_gant__2015-05-21 12:42:11'>Load</a><a href='' class='button delete-gant red' data-gantname='_GANT_this_is_gant__2015-05-21 12:42:11'>Delete</a></td></tr><tr><td>bunda</td>"+ 
    "<td>2015-05-21 12:47:36</td>"+ 
    "<td><pre>[gant id='139']</pre></td>"+ 
    "<td><a href='#' class='button load-gant' data-gantname='_GANT_bunda__2015-05-21 12:47:36'>Load</a> <a href='' class='button delete-gant red' data-gantname='_GANT_bunda__2015-05-21 12:47:36'>Delete</a></td></tr>"; 
相關問題