json字符串中的HTML导致错误

时间:2015-05-21 13:23:54

标签: php json

我在PHP脚本中有以下简单的json。

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 lint继续说

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

我所做的是试图逃避每个双引号,并使用htmlentities()转义每个标签。但是,lint说同样的话。

问题是什么,以及如何解决这个问题?

3 个答案:

答案 0 :(得分: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>

只需将值保留在一行中,并将所有"替换为'

答案 1 :(得分: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 lint

有效

答案 2 :(得分: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>";