如何获取json格式的数据

时间:2016-05-27 09:35:59

标签: php jquery json

我想以json格式获取数据。但我无法获得这些数据。数据采用这种格式

{"make":"VOLKSWAGEN","dateOfFirstRegistration":"23 July 2009","yearOfManufacture":"2009","cylinderCapacity":"1968cc","co2Emissions":"167 g/km","fuelType":"DIESEL","taxStatus":"Tax not due","colour":"SILVER","typeApproval":"M1","wheelPlan":"2 AXLE RIGID BODY","revenueWeight":"Not available","taxDetails":"Tax due: 01 October 2016","motDetails":"Expires: 28 April 2017","taxed":true,"mot":true,"vin":"WVGZZZ5NZAW007903","model":"Tiguan","transmission":"Manual","numberOfDoors":"5","sixMonthRate":"","twelveMonthRate":""}

我试过这个。

$response = curl_exec($curl);
$data=json_decode($response,TRUE);
echo $data->make;

5 个答案:

答案 0 :(得分:1)

试试这个:

echo $data['make'];

因为json_decode将数据转换为关联数组,而不是转换为std对象数组。

答案 1 :(得分:1)

<?php
   $json = '{"make":"VOLKSWAGEN","dateOfFirstRegistration":"23 July 2009","yearOfManufacture":"2009","cylinderCapacity":"1968cc","co2Emissions":"167 g/km","fuelType":"DIESEL","taxStatus":"Tax not due","colour":"SILVER","typeApproval":"M1","wheelPlan":"2 AXLE RIGID BODY","revenueWeight":"Not available","taxDetails":"Tax due: 01 October 2016","motDetails":"Expires: 28 April 2017","taxed":true,"mot":true,"vin":"WVGZZZ5NZAW007903","model":"Tiguan","transmission":"Manual","numberOfDoors":"5","sixMonthRate":"","twelveMonthRate":""}
';

   var_dump(json_decode($json));
   var_dump(json_decode($json, true));
?>

答案 2 :(得分:0)

您必须使用数组键而不是对象从json_decode中获取值。

尝试:

echo $data["make"]

答案 3 :(得分:0)

将json字符串作为对象返回

$json_obj = json_decode($response);
    print_r("<pre>");
    print_r($json_obj->make);

将json字符串作为数组返回

$data=json_decode($response,TRUE);  
    print_r("<pre>");
    print_r($data['make']);

答案 4 :(得分:0)

你可以尝试

$response = curl_exec($curl);
$data=json_decode($response,TRUE);
echo $data['make'];