Reputation: 447
Actually this is a string,
How can i convert it to object or array ..
{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}
I tried this
$array=explode(' ',$result);
$json = json_encode($result);
print($json);
It is giving me the result like this
"{\"kind\": \"delivery_quote\", \"fee\": 750, \"created\": \"2016-02-28T19:13:38Z\", \"expires\": \"2016-02-28T19:18:38Z\", \"currency\": \"usd\", \"duration\": 60, \"dropoff_eta\": \"2016-02-28T20:18:38Z\", \"id\": \"dqt_KhC5sbjq00Jn6F\"}"
But how can i do this properly so that i can take the results like
echo $json->fee;
Here is the Eval of what i have so far.
Help pls
Upvotes: 0
Views: 34
Reputation: 7987
You are trying to encode the json. json_encode Returns the JSON representation of a value. If you want to convert json to array then you should use json_decode as :
$result = '{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}';
$json = json_decode($result);
echo $json->fee;
Which will give the value of fee as output :
750
Upvotes: 2
Reputation: 4782
<?php
$result = '{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}';
var_dump(json_decode($result));
var_dump(json_decode($result, true));
Upvotes: 1