PHP复杂的字符串解析,JSON'able?(PHP Complex String Pars

2019-09-17 07:38发布

所以,我有以下的PHP字符串:

$output = {"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians' official Twitter feed reports.","spin":"He'll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}

我需要的是:“阿卡多是从哥伦布周一,印第安人的官方Twitter的饲料报告叫了起来。” 和“他会就背着2.76时代在13次与快船在赛季开始后的活性名册代替丹惠勒。” 如子。 但我似乎无法弄清楚做到这一点的最好的和最优雅的方式。 我试图JSON_decode绳子,但我什么也没得到恢复。 有任何想法吗? (我使用PHP)

Answer 1:

你有几个转义字符串,导致错误。 一个简单的格式可以挽救你的时间。

$output = '{
    "playerId":1178,
    "percentChange":0.1,
    "averageDraftPosition":260,
    "percentOwned":0.1,
    "mostRecentNews": {
        "news":"Accardo was called up from Columbus on Monday, the Indians official Twitter feed reports",
        "spin":"Hell replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.",
        "date":"Mon May 14"
    },
    "fullName":"Jeremy Accardo"
}';
$json = json_decode($output);


Answer 2:

这不是一个string 。 尝试这样的:

$output = '{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians\' official Twitter feed reports.","spin":"He\'ll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}';

$object = json_decode($output);
$array = json_decode($output, true);
$string = json_encode($array);


Answer 3:

你试试这个?

$output = '{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians\' official Twitter feed reports.","spin":"He\'ll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}';

$array = json_decode($output, true);

echo $array['mostRecentNews']['news'];
echo $array['mostRecentNews']['spin'];


Answer 4:

json_encode与只有UTF8。 您使用与UTF8 allthings? 你哈瓦synstax错误。 如果你定义JSON变量手动也可以是这样的;

<?php
$output=<<<JSONSTR
{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians' official Twitter feed reports.","spin":"He'll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}
JSONSTR;
$variable = json_decode($output);
var_dump($variable);
?>


文章来源: PHP Complex String Parse, JSON'able?