user2650277 user2650277 - 1 month ago 22
JSON Question

Convert json to array php gives Array to string conversion

I have a json as shown below.I want to access the value of

adult
.However when i do
echo json_decode($json_response, true);
i get
Array to string conversion
.What is wrong here ?

{
"responses": [
{
"safeSearchAnnotation": {
"adult": "VERY_UNLIKELY",
"spoof": "VERY_UNLIKELY",
"medical": "UNLIKELY",
"violence": "LIKELY"
}
}
]
}

Answer

The function json_decode returns an array. You can't echo an array, or you'll get that conversion error.

You want to use print_r instead:

print_r(json_decode($json_response, true));

See here: https://3v4l.org/K3UfP