2013-05-08 83 views
0
$result = json_decode(file_get_contents('route.json'),true); 

// the json file is here: http://myweb.polyu.edu.hk/~11010482d/FSP/route.json 

print_r($result); 

//it show '[{"X":"264","Y":"115"},{"X":"328","Y":"115"},{"X":"309","Y":"216"},{"X":"256","Y":"222"},{"X":"227","Y":"217"},{"X":"227","Y":"238"},{"X":"223","Y":"221"},{"X":"223","Y":"205"},{"X":"254","Y":"206"},{"X":"309","Y":"182"},{"X":"309","Y":"98"},{"X":"327","Y":"98"}]' 

//i have tried the string not using $result as variable to decode and it works. 

$abcdefg = json_decode('[{"X":"264","Y":"115"},{"X":"328","Y":"115"},{"X":"309","Y":"216"},{"X":"256","Y":"222"},{"X":"227","Y":"217"},{"X":"227","Y":"238"},{"X":"223","Y":"221"},{"X":"223","Y":"205"},{"X":"254","Y":"206"},{"X":"309","Y":"182"},{"X":"309","Y":"98"},{"X":"327","Y":"98"}]',true); 

print_r($abcdefg); 

//it show Array ([0] => Array ([X] => 264 [Y] => 115) [1] => Array ([X] => 328 [Y] => 115) [2] => Array ([X] => 309 [Y] => 216) [3] => Array ([X] => 256 [Y] => 222) [4] => Array ([X] => 227 [Y] => 217) [5] => Array ([X] => 227 [Y] => 238) [6] => Array ([X] => 223 [Y] => 221) [7] => Array ([X] => 223 [Y] => 205) [8] => Array ([X] => 254 [Y] => 206) [9] => Array ([X] => 309 [Y] => 182) [10] => Array ([X] => 309 [Y] => 98) [11] => Array ([X] => 327 [Y] => 98)) 
// and i want this result for the previous way. 
+0

你'route.json'似乎并没有一个有效的JSON - 它是由一个单引号包裹。所以这最多只是一个**字符串**。删除单引号并重试。 – Passerby 2013-05-08 04:14:51

+0

它可能是jsonp正在等待父索引?只是一个猜测:) – 2013-05-08 04:16:16

+0

[我希望你有这个问题,希望这有助于] [1] [1]:http://stackoverflow.com/questions/6336174/string-appears-to- be-valid-json-but-json-decode-returns-null – 2013-05-08 04:21:23

回答

0

解码JSON字符串,请试试这个:

<?php 
$json=file_get_contents('http://myweb.polyu.edu.hk/~11010482d/FSP/route.json'); 
$json=substr($json,1,-1); 

$result = json_decode($json,true); 

print_r($result); 
?> 
+0

这可能是很好的提供和解释了。数据源具有不需要的前导和尾随引号。对substr的调用删除它们。 – bumperbox 2013-05-08 04:17:21

+0

哈哈只是为了加快速度,但是'substr'切断了第一个和最后一个字符。 – 2013-05-08 04:19:49

0

试试这个

<?php 
$string = file_get_contents('http://myweb.polyu.edu.hk/~11010482d/FSP/route.json'); 
$result = json_decode(trim($string,"'"),true); 
print_r($result); 
?> 

实际的问题是从URL您的JSON周围有报价..这使得它无效的JSON ..