2

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 ?

asked May 9, 2016 at 6:18
2
  • Please try to convert it in array using json_decode($response_array); Commented May 9, 2016 at 6:22
  • First, the response is not well formatted Commented May 9, 2016 at 6:22

3 Answers 3

2

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;
answered May 9, 2016 at 6:24

Comments

0

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.

answered May 9, 2016 at 6:36

7 Comments

how can i add double quote to each parameter of response ?
@NiraliJoshi you've to manually add double qoutes. According to JSON, all string must be surounded by double qoutes, otherwise, it might treat it as a variable and result in error.
@NiraliJoshi where are you getting the response from?
response is from i-tel switch api. they decline to accept that their response data is not in proper json format
@NiraliJoshi Try this code on the response before using json_decode (). $json = preg_replace('/([{,])(\s*)([A-Za-z0-9_\-]+?)\s*:/','1ドル"3ドル":',$json);
|
0

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);
B V Raman
2122 silver badges17 bronze badges
answered May 9, 2016 at 6:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.