使用json_decode()函数解码html标签

时间:2010-10-01 08:07:11

标签: php html json

我正在尝试使用PHP中的json_decode()解码JSON字符串。我的JSON字符串的一部分包含HTML标记。例如。 (为了更好地查看代码,请转到http://gist.github.com/605906

$json = '{"productid" : "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950","memo" : "<div style=\"color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-image: initial; background-repeat: initial; background-attachment: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;\">WS1CI metered clack valve, 1\" for softener  P/N: V1CIDME-03</div>"}';

But I am getting NULL values, when I do

var_dump(json_decode($json));

or

$json = '{"productid" : "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950","memo" : "<div style=\"color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-image: initial; background-repeat: initial; background-attachment: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;\">WS1CI metered clack valve, 1\" for softener  P/N: V1CIDME-03</div>"}';

3 个答案:

答案 0 :(得分:1)

它对我有用。如果你得到一个特定的错误,请检查你的PHP安装,但是从PHP 5.2.0开始,Json默认出现。

object(stdClass)#99 (2) {
  ["productid"]=>
  string(41) "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950"
  ["memo"]=>
  string(59) "WS1CI metered clack valve, 1" for softener  P/N: V1CIDME-03"
}
array(2) {
  ["productid"]=>
  string(41) "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950"
  ["memo"]=>
  string(59) "WS1CI metered clack valve, 1" for softener  P/N: V1CIDME-03"
}

甚至适用于html。

object(stdClass)#1 (2) {
  ["productid"]=>
  string(41) "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950"
  ["memo"]=>
  string(328) "<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-image: initial; background-repeat: initial; background-attachment: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;">WS1CI metered clack valve, 1" for softener  P/N: V1CIDME-03</div>"
}

答案 1 :(得分:0)

查看服务器的error_log。这可能会产生更多信息。

答案 2 :(得分:-3)

<?php
$arr = array();
$json = '{"productid" : "prod:a8f2d4ef-108e-5fbf-fa74-595ddc0c7950","memo" : "&lt;div style=\"color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-image: initial; background-repeat: initial; background-attachment: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;\"&gt;WS1CI metered clack valve, 1\" for softener  P/N: V1CIDME-03&lt;/div&gt;"}';


var_dump(json_decode($json, true));

?>

您需要将所有html(例如<>)转换为&lt;&gt;