Variables, arrays and objects are a familiar and convenient form of representing data. It is customary to describe data in the JavaScript browser language, which is not necessary in the PHP server language. The JSON format allows you to generalize them into one whole and not to emphasize the programming language. In this case, the data turns into pairs: "name = value". The value in each of them can also be a collection of such pairs.
It is accepted to associate JSON with curly braces and JavaScript objects. The latter is quite justified, since the format is JSON = Object Notation JavaScript. In recent particularly dynamic years, much has changed. What was created for a specific purpose often brought unexpected results or opened up new horizons.
Data exchange: browser - server
AJAX technology has become traditional, the usual page refresh completely ceased to be popular. When a visitor opens a site, he initiates a series of partial data exchanges when certain pages change only in the place that is relevant.
It is believed that the appearance of JSON is associated with the use of AJAX, but in fact associative arrays of JavaScript and its object notation (features of the syntax for describing and using objects) are much more closely related to JSON than the exchange of data between the browser and the server.
Since the content of the pages of modern sites has really become “massive” (voluminous), the format efficiency for data exchange has gained particular importance. This is not to say that JSON has become a new representation of data, but the fact that it has long been an element of JavaScript syntax is significant.
The use of Cyrillic in naming variables is a very unexpected phenomenon (nonsense), but it works in the latest versions of Chrome, Firefox, and even Internet Explorer 11.
Cyrillic and JSON
Of course, you should not use this completely unexpected phenomenon, recalling how easily the values of variables written in Russian letters turn into krakozyabry: what can we say about names, especially external ones.
It is doubtful that the initiative in Cyrillic names will be supported by the external environment of the browser, with which he constantly has to deal. But this fact deserves attention for the simple reason that the JSON format is the ability to write names and values in any way the developer wants. This is important, since in each task the description of the field of application as it requires it greatly simplifies debugging and reduces the number of errors.
It doesn’t matter exactly what the syntax innovation JSON was based on, it is important that it gave a legitimate right and a real opportunity to establish the correspondence: “any name = any value”.
We must pay tribute to the JavaScript language: what is provided by the syntax does not oblige the developer and does not impose anything on him. The developer freely uses the syntax of the language for the optimal formation of the data model and the algorithm for their use.
PHP and JSON
By accepting data in JSON format, the server (through PHP, in particular) provides the ability to process it as is and return the result back to the browser in a similar format. PHP source array:
- $ cJSON = array ("a" => "alfa", "b" => "beta", "g" => "gamma").
Convert to JSON format for transfer to browser:
- $ cJS = json_encode ($ cJSON ).
Result:
- {"a": "alfa", "b": "beta", "g": "gamma"}.
The nesting shown in the photo is allowed.
Here, the formed array was added with a new “in itself” element with the automatic index “0”, and then again with the specified index “z”.
The inverse function is json_decode (), which converts a JSON string into a PHP array. Similar results can be achieved by manipulating the functions of PHP: implode () and explode (). In some cases, this option is preferable.
Nesting level
Elements can be nested within each other both on the browser side and on the server side. In practice, the JSON format (RFC 4627 standard description) provides significantly more than 4 levels of nesting, but you should not abuse this feature.
It’s best to never go beyond reasonable sufficiency, this makes the code readable, simplifies debugging and understanding by other developers.
JSON is usually referred to as data constructs that are simpler than XML, understandable both to people and computers. This is true when the amount of data is small, and the developer has chosen the level of nesting correctly. In all other cases, it is difficult to count the number of brackets and understand the data structure both on the browser side and on the server side.
JSON Files
Putting JSON into practice is often not limited to short-string perception. Any data constructs are always pragmatic. At the same time, JSON can be effectively applied both in real task data (enterprise staff) and for implementing temporary data (object cache).
Enterprise staff and JSON format: example
Typically, a record of one person is a surname, first name, middle name, year of birth, specialty, education, ... and a few more simple meanings. Even in particularly demanding companies, a record of one person will not exceed a dozen or two fields. It is perceptible and can be placed in a database row.
If a company employs several people, this is one thing, but if tens of thousands are completely different. You can continue to use the database, but storing it as a file looks more practical and more accessible for use.
The JSON file format is a plain text file. The staffing case wherever it went. You can always read it. Open and edit is also available in any text editor that does not have the habit of adding its service information to the file contents. In general, * .json is pure text both inside the browser and inside the file - a string.
The photo shows the cache of the object forming the picture, an example.
This is an example of the contents of a file generated by a site that provides color volumetric printing services for mugs and ceramic products. Naturally, deciding with such a JSON format than opening it is really problematic. However, in this and similar cases there are no problems with reading the file: PHP reads the file, parses it, and transfers it to the browser. The data changed by the visitor is returned to the server and written back.
In this use case, the file acts as a variable that is stored outside the code. If necessary, the variable receives the value from the file, and if it is changed by the visitor in the dialog provided by the site, then all changes will be recorded as is. No need to read and check the contents of the file.
JSON is often used to store and use service information - this is not a staffing table, it is not necessary to see either the developer or the site visitor.
XML and JSON
“Everything has its time” - classical knowledge, accepted as an axiom even before the advent of programming. “Nothing appears just like that,” - this also happened before a person wrote the first intelligible program in an artificial language.
Data formats arise from real needs and are based on knowledge gained. HTML has its own path, XML has its own way, and JSON is JavaScript object logic extended to other languages. Comparing one with the other is not the best thing. To each his own.
XML does its job wonderfully and is clearly not going to go down in history. And JSON was used until 2006, just not every developer considered it his duty to declare certain options for the presentation of their data.
There were cases in practice when programs were written in BASIC that did not use JSON as such, but perfectly stored name-value pairs and put them at the disposal of the right algorithms at the right time.
Special characters ("` "," ~ "," | ", ...) and data formats
The habit of working with associative arrays and objects in JavaScript makes using JSON natural and convenient. This is a really great format, but the ability to split and join, manipulating strings and arrays, has much deeper roots.
The join / split functions of the JavaScript language and the implode / explode language of the PHP language allow you to conveniently and efficiently use both XML, JSON data formats, and your own version. The latter is often optimal, and the first two are ideal for common use options. If information is being transferred to another developer, server, file or database, it is better not to find XML and JSON. Everyone works with them, because the transmission / reception of information does not need comments.
Using JSON in Android
Reading and writing data in the JSON format in Android is not only the norm of things, but also a lot of objects focused on working with this particular data format.
Many attribute the phenomenal success of social networks to the fact that members and visitors to relevant sites (Facebook, LinkedIn, Twitter, ...) benefit from the materials published there.
In this case, it is the JSON format that is used. Perhaps this is so, but the question is not the phenomenality of social networks, but that really presenting information in the format "name = value" is convenient both for programming and for use. Unlike strict and complex XML, this is a truly human-friendly format.
Associative arrays
It so happened that the variables should be described (JavaScript) or at least indicate the initial value (PHP). In both cases, a variable can change its type very easily. If necessary, the language performs this conversion automatically.
But why not also change the name of the variable, not appear during the execution of the algorithm and not disappear when the need for it disappears? Associative arrays allow solving this problem, but then when using such relatively dynamic variables, the name of the array and the corresponding syntactic constructions will follow in the places of their application.
This circumstance is especially pronounced in PHP, but you can put up with this, as, however, with the symbol "$" in the variable name and the combination "$ this->" inside the object. When programming in JavaScript and PHP at the same time, at first you really wonder how everything is different, but then everything becomes so familiar and natural ...
Associative Array -> JSON
In this example, a * .docx document is created using the PHPOffice / PHPWord library, and the aProperties array contains the properties of this document (author, company, title, category, creation date ...).
The second array contains page data:
- orientation (landscape or normal);
- vertical and horizontal dimensions;
- indents (margins on the left, top, bottom, right);
- headers and footers.
The document is generated on the server where the PHPOffice / PHPWord library is installed. The site provides control over the values of these arrays via JavaScript. The result in JSON format is returned back to the server and used in PHP algorithms, in its constructions, that is, in arrays.
Dynamic variables
The JSON format solves the problem of dynamic variables. Here you can create, modify and delete variables without unnecessary syntactic heaps. It looks beautiful and is used in JavaScript.
In this example, the GetOjInfo () function selects the value name and value from the object. Initially, the JSON string object assigned to the ojInfo variable has three elements: Name, age, and work. A bit later, the Status variable is added.
After the first delete statement, ojInfo loses the age element, after the second delete, the work element. If we assume that this line is a selection of variables that have a certain meaning, then using JSON you can actually create, modify and delete any of their sets outside the operating field (syntax) of the description and processing of the JavaScript language.
The JSON format for this option was not designed, but this is possible, practical and convenient.