PHP 如何读取如下的JSON数据?
{status:0,message:"queryok",result:{elements:[{from:{lat:37.05983,lng:114.46816},to:{...
{
status: 0,
message: "query ok",
result: {
elements: [
{
from: {
lat: 37.05983,
lng: 114.46816
},
to: {
lat: 37.069569,
lng: 114.493048
},
distance: 3369,
duration: 536
}
]
}
}
我只想拿到 distance 数据 展开
status: 0,
message: "query ok",
result: {
elements: [
{
from: {
lat: 37.05983,
lng: 114.46816
},
to: {
lat: 37.069569,
lng: 114.493048
},
distance: 3369,
duration: 536
}
]
}
}
我只想拿到 distance 数据 展开
2个回答
展开全部
php内置json编码和解析函数
$response=file_get_contents('https://apis.map.qq.com/ws/location/v1/ip?ip=120.239.177.236&key=申请的QQ地图密钥');
//把字符串解析为数组,第二个参数默认false,解析为对象
$json = json_decode($response,true);
echo $json['location']['lng'];
echo $json['location']['lat'];
//把数组编码成字符串输出,第二个参数为不编码unicode字符(汉字就属于unicode,不编码则原样显示,编码后为 \uxxxx 的格式)
echo json_encode($json,JSON_UNESCAPED_UNICODE);
展开全部
<?php
$tmp = '{"status":0,"message":"query ok","result":{"elements":[{"from":{"lat":37.05983,"lng":114.46816},"to":{"lat":37.069569,"lng":114.493048},"distance":3369,"duration":536}]}}';
$arr = json_decode($tmp,true);
print_r($arr["result"]["elements"][0]["distance"]);
$tmp = '{"status":0,"message":"query ok","result":{"elements":[{"from":{"lat":37.05983,"lng":114.46816},"to":{"lat":37.069569,"lng":114.493048},"distance":3369,"duration":536}]}}';
$arr = json_decode($tmp,true);
print_r($arr["result"]["elements"][0]["distance"]);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询