2012-05-17 77 views
1

所以我有以下的PHP字符串:PHP复杂字符串解析,JSON'able?

$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的饲料报道叫了起来”和“在与快船开始本赛季13次出场后,他将在主动阵容中替换Dan Wheeler。”作为子串。但我似乎无法弄清楚最好和最优雅的方式来做到这一点。我试图对JSON_decode字符串进行解码,但没有返回任何内容。有任何想法吗? (我正在使用PHP)

+2

你能给我们一段你正在用来解析它的代码吗?另外,请注意,json_decode有第二个变量,可以让你选择它是一个数组还是一个对象。 – Odinn

+0

@ sfgiants2010您的json数据包含许多未转义的单引号,我已修复它,检查我的答案。 –

回答

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); 
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']; 
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); 
0

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); 
?>