Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use PHP JSON extension correctly

Shulou Source: shulou.com Published: 2022-06-03 19:03:39 11月02日 Update

This article introduces the knowledge of "how to use PHP JSON extension correctly". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

JSON is a protocol designed to allow middleware to create objects that use the inherent format of JavaScript. The big attribute of it is that it is a lightweight protocol. When simply dealing with RSS aggregations or recipe lists, you don't need to use all the features of XML in JavaScript. There is no need to validate the format or ensure strict data typing.

PHP JSON extended encoding and decoding

There are two functions for PHP JSON extensions: encode and decode. * functions will convert any type of data object into a set of serialized data for JavaScript to process. The second function will decode the serialized data and convert it to a basic PHP object or associative array. Let's look at json_decode ().

Example of json_decode ()

< ?php $jsonObject = '{"21":{"url":"www.blah.com \/story1.html","title":"JSON is sweeping AJAX world","viewed":false},"22":{"url": "www.blah.com\/story2.html","title":"JSON is great","viewed":false}}'; $decodedObject = json_decode($jsonObject); $decodedArray = json_decode($jsonObject, true); print_r($decodedObject); echo "< br>

< br>

"

Print_r ($decodedArray)

? >

As above, we have a PHP script that takes $jsonObject and decodes it back to the PHP native object. We decoded it twice. * times, using unmodified usage, this will get the object of stdClass; the second time, use Boolean parameters to create associative arrays.

The following is the output of decode:

StdClass Object ([21] = >

StdClass Object ([url] = >

Www.blah.com/story1.html [title] = >

JSON is sweeping AJAX world [viewed] = >

) [22] = > stdClass Object ([url] = >

Www.blah.com/story2.html [title] = >

JSON is great [viewed] = >) Array ([21] = >

Array ([url] = > www.blah.com/story1.html

[title] = > JSON is sweeping AJAX world

[viewed] = >) [22] = > Array ([url] = >

Www.blah.com/story2.html [title] = >

JSON is great [viewed] = >))

Let's take a look at encode:

< ?php $results = array("21" =>

Array ("url" = > "www.blah.com/story1.html", "title" = > "JSON is sweeping AJAX world", "viewed" = > FALSE), "22" = > array ("url" = > "www.blah.com/story2.html", "title" = > "JSON is great", "viewed" = > FALSE)); $jsonObject = json_encode ($results); echo $jsonObject;? >

No recursion is used. No tags have been added. Just pass it into the json_encode () function, and it will pass out as a JSON serialized object from the other end.

That's all for "how to use PHP JSON extensions correctly". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Objects functions data sequences contents arrays more formats knowledge scripts and will processing federation output practical learned and then one end two middleware Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple MySQL Huawei Linux