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"
}
}
]
}
Stringify a JavaScript ArrayUse the JavaScript function JSON.stringify() to convert it into a string.
The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.
JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With