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.
4

2 回答 2

0

请试试这个:

<?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);
?>
于 2013-05-08T04:14:39.790 回答
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 ..

于 2013-05-08T04:17:13.743 回答