Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法按变量解码json字符串_Php_Arrays_Json - Fatal编程技术网

Php 无法按变量解码json字符串

Php 无法按变量解码json字符串,php,arrays,json,Php,Arrays,Json,请试试这个: $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":

请试试这个:

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

试试这个

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

您的
route.json
似乎不是有效的json-它由一个引号包装。所以这充其量只是一条线。请删除单引号,然后重试。可能是jsonp正在等待父索引?只是一个猜测:)[我希望你有这个问题,希望这有帮助][1][1]:提供和解释可能也不错。数据源具有不需要的前导引号和尾随引号。对substr的调用删除了它们。哈哈,只是为了速度,但是是的
substr
删除了第一个和最后一个字符。
<?php
$string =  file_get_contents('http://myweb.polyu.edu.hk/~11010482d/FSP/route.json');
$result = json_decode(trim($string,"'"),true);
print_r($result);
?>