I have a response data like,
$response_array= "{status: 'false',description: '5100|4567897845'}"
json validator says it is a valid JSON.
but when i try to access status parameter
echo $response_array->status;
it outputs nothing.
How can i get status value ?
-
Please try to convert it in array using json_decode($response_array);Manmohan– Manmohan2016年05月09日 06:22:04 +00:00Commented May 9, 2016 at 6:22
-
First, the response is not well formattedThamilhan– Thamilhan2016年05月09日 06:22:43 +00:00Commented May 9, 2016 at 6:22
3 Answers 3
You have got bad quotes in your JSON string, try to edit string to this:
$response_array= '{"status": "false","description": "5100|4567897845"}';
Then you can use json_decode()
function, like:
$response_array = json_decode('{"status": "false","description": "5100|4567897845"}');
echo $response_array->status;
Comments
Please use json_decode which is used to decode a JSON formatted string. Hence, it is always advisable for string elements in a JSON pair to be enclosed within double qoutes. Hence, please format the response array in said format. Then you could simple use json_decode and access respective Key Value Pair.
$response_array= '{"status": "false","description": "5100|4567897845"}';
$obj = json_decode( $response_array);
echo $obj->description;
Hope this helps.
7 Comments
for php return json value is a simple string you need to decode them using
json_decode
$response = json_decode($response_array);
if it returns as an object you can access using
$response->status
or if it's an array then you can access like$response['status']
For it to be returned as an array, the second parameter in json_decode has to be set to true.
$response = json_decode($response_array, true);