ComputersProgramming

JSON format: description, example

Variables, arrays and objects are a familiar and convenient form of data representation. It is customary to describe the data in the JavaScript browser language, which is not necessary in the PHP server language. The JSON format makes it possible to generalize them into one and not to focus on 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 customary to associate JSON with curly braces and JavaScript objects. The latter is fully justified, since the JSON = Object Notation JavaScript format. In the last particularly dynamic years much has changed. What was created for a specific purpose, often brought unexpected fruit or opened new horizons.

Data exchange: browser - server

AJAX technology has become traditional, the usual updating of the entire page has ceased to be popular. A visitor, when opening a site, initiates a series of partial data exchanges, when one or another page changes only in the place that is relevant.

It is believed that the emergence of JSON is due to the use of AJAX, but in fact, the associative arrays of JavaScript and its object notation (the syntax of describing and using objects) are much more closely related to JSON than the data exchange between the browser and the server.

Since the content of the pages of modern sites has really become "massive" (volumetric), then the efficiency of the format for data exchange has acquired special significance. It can not be said that JSON has become a new data representation, but the fact that it has long been an element of JavaScript syntax is essential.

Using Cyrillic in variable naming is a very unexpected phenomenon (nonsense), but it works in the latest versions of Chrome, Firefox and, even, in Internet Explorer 11.

Cyrillic and JSON

Of course, it's not worth using this completely unexpected phenomenon, remembering how easily the values of variables written in Russian letters turn into krakozyabry: what is there to 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 the way a developer wants. This is important, because in each task the description of the application area as it requires it greatly simplifies the debugging and reduces the number of errors.

It does not matter what the syntactic innovation JSON was based on, it's important that it gave a legal right and a real opportunity to put a match: "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 using them.

PHP and JSON

Taking the data in JSON format, the server (through PHP in particular) provides the ability to process them 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 for transfer to the browser:

  • $ CJS = json_encode ($ cJSON ).

Result:

  • {"A": "alfa", "b": "beta", "g": "gamma"}.

The nesting shown in the photo is allowed.

Here the array was added by a new element "in itself" with the automatic index "0", and then again with the specified index "z".

The inverse function, json_decode (), converts the JSON string to a PHP array. Similar results can be achieved by manipulating PHP functions: implode () and explode (). In some cases, this option is preferable.

Level of nesting

Elements can be nested to each other both on the browser side and on the server side. In practice, the JSON format (description of RFC 4627) provides significantly more than 4 levels of nesting, but you should not abuse this feature.

It is best never to go beyond reasonable sufficiency, this makes the code readable, simplifies its debugging and understanding by other developers.

JSON is usually referred to data structures that are simpler than XML, are understood simultaneously by people and computers. This is true when the amount of data is small, and the level of nesting the developer has chosen competently. In all other cases, counting the number of parentheses and understanding the data structure is difficult both on the browser side and on the server side.

JSON Files

The use of JSON in practice is often not limited to an accessible short line. Any data constructs are always pragmatic. In this case, JSON can be effectively applied both in real task data (enterprise staff) and for the implementation of temporary data (object cache).

Enterprise staff and JSON-format: example

Usually a record of one person is a surname, first name, patronymic, year of birth, specialty, education, ... and a few more simple meanings. Even in particularly demanding companies, the record of one person will not exceed a dozen or two fields. This is accessible to perception and can be placed in a database line.

If there are several people in the company, this is one thing, but if tens of thousands is 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 case with the staff list, 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 content. Generally, * .json is pure text both inside the browser and inside the file - a string.

The picture shows the cache of the object that forms the picture, an example.

This is an example of the contents of a file generated by a site providing a color volumetric printing service on mugs and ceramic products. Naturally, it's really problematic to decide if you have such a JSON format than to open it. However, in this and similar cases there are no problems with reading the file: PHP reads the file, parses it and sends it to the browser. The data changed by the visitor is returned to the server and recorded back.

In this case, the file acts as a variable that is stored outside the code. If necessary, the variable receives a value from the file, and if it is changed by the visitor in the dialogue provided by the site, then all changes will be recorded as is. There is no need to read and check the contents of the file.

JSON is often used to store and use the service information - this is not a staffing list, it is not necessary to see it either by the developer or by the site visitor.

XML and JSON

"Everything has its time" - the classical knowledge accepted for the axiom even before the advent of programming. "Nothing appears just like that" - it was also before a person wrote the first intelligible program in an artificial language.

Data formats appear from the real need and are based on the knowledge gained. HTML has its own path, XML has its own road, and JSON is the JavaScript object logic extended to other languages. Comparing one with another is not the best thing. To each his own.

XML miraculously cope with its tasks and go into history is clearly not going to. And JSON was used even before 2006, it's just that not every developer considered it his duty to declare certain variants of presenting his data.

There have been cases in practice when basic programs were written in BASIC that do not use JSON as such, but perfectly store the "name = value" pairs and give them the right algorithms at the right time.

Special characters ("` "," ~ "," | ", ...) and data formats

The habit of working with associative arrays and JavaScript objects makes the use of JSON natural and convenient. This is really a great format, but the ability to split and merge, manipulating strings and arrays, has far deeper roots.

The join / split functions of the JavaScript language and implode / explode of the PHP language make it convenient and efficient to use both XML data formats, JSON, and your own version. The latter is often optimal, and the first two are ideal for general use. If there is a transfer of information to another developer, server, file or database, XML and JSON are not better. With them all work, because the transmission / reception of information does not need comments.

Using JSON on Android

Reading and writing data in JSON format in Android is not only the norm of things, but also a lot of objects oriented to work with this data format.

Many associate the phenomenal success of social networks with the fact that members and visitors to relevant sites (Facebook, LinkedIn, Twitter, ...) benefit from the materials published there.

In this case, the JSON format is used. Perhaps this is true, but the question is not the phenomenal nature of social networks, but the fact that the presentation of information in the format "name = value" is convenient both for programming and for use. Unlike strict and complex "XML", this is really a human-friendly format.

Associative arrays

It turned out that the variables should be described (JavaScript) or at least specify the initial value (PHP). In both cases, the variable can change its type very easily. If necessary, the language performs this conversion automatically.

But why should not the variable also change its name, not appear during the execution of the algorithm and not disappear, when the need for it will disappear? Associative arrays allow to solve this problem, but then using such relatively dynamic variables, the array name and the corresponding syntactic constructions will follow to the places of their application.

Especially this circumstance is clearly expressed in PHP, but this can be tolerated, as, however, with the symbol "$" in the variable name and the combination "$ this->" inside the object. Programming at the same time in JavaScript and PHP, at first it's really amazing 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 data on the page:

  • Orientation (landscape or conventional);
  • The sizes on a vertical and horizontal;
  • Indents (fields on the left, from above, from below, on the right);
  • Headers and footers.

The document is created on the server where the PHPOffice / PHPWord library is installed. The site provides management of the values of these arrays through JavaScript. The result in JSON format is returned back to the server and used in PHP algorithms, in its constructs, 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 syntax piles. It looks nice 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 little later, the variable Status is added.

After the first delete statement, the ojInfo string loses the age element, after the second delete, the work element. Assuming that this line is a collection of variables that have a certain meaning, then by means of JSON you can actually create, modify and delete any of their sets outside the operational field (syntax) of describing and processing the JavaScript language.

The JSON format for this option was not calculated, but it is possible, practical and convenient.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.