0

我有一个 json 数据,我试图从该 json 中获取经度和纬度。

这是JSON代码

{
"0": {
    "provider": "gps",
    "time": 1517235370666,
    "latitude": 31.501278877258,
    "longitude": 74.366261959076,
    "accuracy": 63,
    "speed": 2.1400001049042,
    "altitude": 181,
    "bearing": 28.125,
    "locationProvider": 0
    }
}

我想要纬度和经度。我用 json_decode 解析它,但我仍然无法得到。
解析后

 stdClass Object
(
[0] => stdClass Object
    (
        [provider] => gps
        [time] => 1517235370666
        [latitude] => 31.501278877258
        [longitude] => 74.366261959076
        [accuracy] => 63
        [speed] => 2.1400001049042
        [altitude] => 181
        [bearing] => 28.125
        [locationProvider] => 0
    )
)
4

1 回答 1

3

尝试这个

$a = '{
"0": {
    "provider": "gps",
    "time": 1517235370666,
    "latitude": 31.501278877258,
    "longitude": 74.366261959076,
    "accuracy": 63,
    "speed": 2.1400001049042,
    "altitude": 181,
    "bearing": 28.125,
    "locationProvider": 0
    }
}';

$b = json_decode($a); // returns object
print_r($b->{'0'}->{'latitude'}); // to get latitude value

 $c = json_decode($a,true); // returns array
 print_r($c[0]['latitude']); // to get latitude value
于 2018-01-29T14:37:26.400 回答