2013-05-08 67 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 ..