我可以将PHP中的JSON解码为数组,但当解码为数组时,JSON中的一些数据会消失。
这是我的JSON文件
[
{
"name": "Games1",
"price": "€ 25.53",
"platform": "<span class=\"platform battle-net\"></span>",
"region": "GLOBAL",
"url": "localhost"
},
{
"name": "Games2",
"price": "€ 24.99",
"platform": "<span class=\"platform xbox-live\"></span>",
"region": "GLOBAL",
"url": "localhost"
}
]这是我的php代码
$data = file_get_contents("game.json");
for ($i = 0; $i <= 31; ++$i) {
$data = str_replace(chr($i), "", $data);
}
$data = str_replace(chr(127), "", $data);
if (0 === strpos(bin2hex($data), 'efbbbf')) {
$data = substr($data, 3);
}
$data = json_decode($data,true);
print_r($data);我的结果来自print_r($data);
Array ( [0] => Array ( [name] => Games1 [price] => € 25.53 [platform] => [region] => GLOBAL [url] => localhost )
[1] => Array ( [name] => Games2 [price] => € 24.99 [platform] => [region] => GLOBAL [url] => localhost ) )我在平台上的价值消失了。有人知道问题出在哪里吗?
发布于 2018-08-09 14:18:06
您的JSON包含HTML标记,当浏览器显示print_r()的结果时,浏览器会解释这些标记。使用浏览器的View Source命令查看原始输出,您应该可以看到跨度。
您还可以使用htmlentities()将它们转换为转义字符,浏览器将按原样显示转义字符。
$output = print_r($data, true);
echo "<pre>" . htmlentities($output, ENT_COMPAT) . "</pre>";使用<pre>也会保持格式。
发布于 2018-08-09 13:36:34
为了将HTML保留在JSON中,您必须遵循多个规则:
在结束标记和自结束标记中
<img ... />
此外,您可以避免复制<span>标记,只需像这样存储它们的类名:"platform": "battle-net",或"platform": "xbox-live",。
https://stackoverflow.com/questions/51759056
复制相似问题