1

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

我需要的是:“印第安人官方推特报道称,周一,阿卡尔多从哥伦布被召来。” “他将在本赛季开始时在快船队出场 13 次以 2.76 的 ERA 取代现役名单上的丹·惠勒。” 作为子串。但我似乎无法找出最好和最优雅的方式来做到这一点。我尝试对字符串进行 JSON_decode,但没有返回任何内容。有任何想法吗?(我正在使用 PHP)

4

4 回答 4

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);
于 2012-05-17T06:34:50.033 回答
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);
于 2012-05-17T06:35:02.497 回答
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'];
于 2012-05-17T06:32:31.717 回答
0

json_encode 仅使用 UTF8。你在用 utf8 吗?你有语法错误。如果您手动定义 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);
?>
于 2012-05-17T06:40:56.317 回答