Reputation: 39
I want to get the data in json format. But I am unable to get that data. Data is in this format
{"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":""}
I have tried this.
$response = curl_exec($curl);
$data=json_decode($response,TRUE);
echo $data->make;
Upvotes: 1
Views: 75
Reputation: 317
<?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));
?>
Upvotes: 1
Reputation: 290
Try with this:
echo $data['make'];
Because json_decode converted the data into associative arrays, not into the std object array.
Upvotes: 1
Reputation: 1
you can try
$response = curl_exec($curl);
$data=json_decode($response,TRUE);
echo $data['make'];
Upvotes: 0
Reputation: 1091
Return json string as object
$json_obj = json_decode($response);
print_r("<pre>");
print_r($json_obj->make);
OR
Return json string as array
$data=json_decode($response,TRUE);
print_r("<pre>");
print_r($data['make']);
Upvotes: 0
Reputation: 11830
You have to fetch value from json_decode using an array key not an object.
Try :
echo $data["make"]
Upvotes: 0