2021年3月27日星期六

How does json_decode and json_encode works?

I am using php to create an API as a plugin for wordpress. And I find it quite hard to manage JSON, compared to js, where JSON.parse() does the work properly. I can't manage to understand this results.

        $body = wp_remote_retrieve_body($res);          $body = json_decode($body, true);          error_log(gettype($body)); // ********string**********          $r = json_encode($body, JSON_FORCE_OBJECT);          error_log(gettype($r)); //  *********string********  

The response from the server is an object like:

{success:true, data: {msg: 'Documents inserted 9', data: [{},{},...]}}

But it doesn't matter what I do, I can't manage into the data.

I checked this website https://linuxhint.com/how-to-parse-json-in-php/ before posting but still can't find a solution

The idea is to check first if success is true, and if it does, then do something with data, if not send an error

https://stackoverflow.com/questions/66838301/how-does-json-decode-and-json-encode-works March 28, 2021 at 12:04PM

没有评论:

发表评论