PHP Complex String Parse,JSON'able?

时间:2012-05-17 06:22:52

标签: php json string parsing

所以我有以下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"}

我需要的是:“周一,印度人的官方推特报道称,阿卡多被哥伦布召集。”并且“他将在快船队名单上取代丹·惠勒(Dan Wheeler),并且在快船队出场13次后出现了2.76自责分率。”作为子串。但我似乎无法找到最好和最优雅的方式来做到这一点。我试着JSON_decode字符串,但我没有得到任何回报。有任何想法吗? (我正在使用PHP)

4 个答案:

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

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

答案 2 :(得分:0)

你试过这个吗?

$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'];

答案 3 :(得分:0)

json_encode只有UTF8。你在utf8上使用allthings吗?你有hava 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);
?>