PHP "Cannot use object of type StdClass as array" (PHP error when calling Json_decode to generate a JSON object from a string object)

Source: Internet
Author: User
Tags php error

PHP calls Json_decode to generate a JSON object from a string object, and if you use the [] operator to fetch data, you get the following error

Error:
Cannot use object of type StdClass as array

Cause:

+ Expand

-php

$res = Json_decode ($res);

$res [' key ']; Use the object after Json_decode () as an array.



Solution (2 types):
1. Use Json_decode ($d, True). is to make the second variable of Json_decode set to true.
2. Json_decode ($res) Returns an object that cannot be accessed using $res [' key '], instead of $res->key.


Reference manual: Json_decode

Return Values:returns An object or if the optional assoc parameter are TRUE, an associative array is instead returned.

The return value is the JSON object by default, and when the second optional argument is true, the array is returned

PHP "Cannot use object of type StdClass as array" (PHP error when calling Json_decode to generate a JSON object from a string object)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.