Practice using the json_decode function

It is convenient to send information from the client to the server in JSON format, since the transmission of GET and POST methods with a large amount of data creates noticeable inconvenience. The JSON format is used everywhere, and the use of the PHP function json_decode is in demand on the server side.

AJAX or popular library

jQuery, AngularJS, Ember.js and other libraries for expanding JavaScript capabilities automatically generate data in JSON format, have tools for processing them and provide the transfer of results to the server. Using the popular library allows you to build on its philosophy, data frame, and ensure the safe and reliable use of the JSON format. Using server-side PHP functions json_decode will be as safe and easy as possible.

JSON data transfer

The data in JSON format has an attractive human-readable appearance, the application experience is quite large, the use of the UTF-8 encoding makes any localization of information possible, including the transformation of data from some regional features of the language to others. Unlike popular libraries, "manual work" through AJAX (XMLHttpRequest) allows you to manipulate the format structure, allows liberties in the description of names and values.

The JSON format is undemanding to the syntax, but nevertheless, when creating data, you should adhere to the usual encoding rules: use curly braces, double quotes and separate names from values ​​with a colon.

JSON String Requirements

General rules for using the format are easy to apply, there are not many of them. The encoding is UTF-8, a string is used to “describe” arrays or objects. The sign of the first is the use of square brackets, the sign of the second is braces. Names and values ​​are separated by a colon and are enclosed in double quotation marks.

JSON format

The programmer “thinks through himself” what to mean by an array and by an object that will be parsed by the PHP function json_decode. Fundamentally, the JSON format came from JavaScript and inherits its ideology of describing arrays and objects. In the context of PHP, which has a completely different representation of object orientation, the responsibility for how to recognize and how to use the json_decode function lies with the programmer.

"Life" of objects and JSON

The fundamental difference between a client and a server, JavaScript and PHP is how the objects “live” in them and the information circulates. If desired, in PHP you can create a mechanism that will function outside the client, but this is exotic! As a general rule, PHP makes a page on the go and each time forms a new system of objects.

JavaScript always works. While the visitor is on the web resource, an object system generated on the server is running in his browser. And it functions constantly until the visitor leaves for another resource.

Client and Server Objects

The system of JavaScript objects can stably form requests to the server, which will be decoded by the json_decode function and make appropriate decisions. It is advisable to build an adequate server model for processing JSON messages and provide an addition to the client implementation of the object system.

Source: https://habr.com/ru/post/K19582/


All Articles