0

Here is my json string:

{
"routes": [
    {
        "bounds": {
            "northeast": {
                "lat": 41.9291739,
                "lng": 23.7293099
            },
            "southwest": {
                "lat": 37.9103506,
                "lng": 12.4822041
            }
        },
        "copyrights": "Map data,google",
        "legs": [
            {
                "distance": {
                    "text": "1,250 km",
                    "value": 1249958
                }
            }
        ]
    }
]
}

i want to get routes->legs->distance->value im using this code unsuccessfully:

$json = json_decode(file_get_contents('./test.txt'));
$distance = $json->routes[0]->legs[0]->distance->value;
var_dump($distance);

any ideas?

4

1 回答 1

2

我认为您在阅读文件时遇到问题,下面的代码有效。

该文件是否存在于同一目录中?你的文件有json内容吗?也许您没有该文件的权限。

<?php

$json=json_decode('{
"routes": [
    {
        "bounds": {
            "northeast": {
                "lat": 41.9291739,
                "lng": 23.7293099
            },
            "southwest": {
                "lat": 37.9103506,
                "lng": 12.4822041
            }
        },
        "copyrights": "Map data,google",
        "legs": [
            {
                "distance": {
                    "text": "1,250 km",
                    "value": 1249958
                }
            }
        ]
    }
]
}');

$distance = $json->routes[0]->legs[0]->distance->value;
var_dump($distance);
于 2013-10-23T13:10:26.210 回答