PHPJSON error: CannotuseobjectoftypestdClassasarray solution

Source: Internet
Author: User
This article mainly introduces the PHPJSON error: CannotuseobjectoftypestdClassasarray solution. if you need to call json_decode to generate a json object from a string object by referring to php, if you use the [] operator to retrieve data, the following error is returned:

The code is as follows:


Cannot use object of type stdClass as array


Cause:

The code is as follows:


$ Res = json_decode ($ res );
$ Res ['key']; // use the object after json_decode () as an array.

Solution (2 types ):

1. use json_decode ($ d, true ). The second variable of json_decode is set to true.
2. json_decode ($ res) returns an object. you cannot use $ res ['key'] for access. you can replace it with $ res-> key.

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.