23.07.2020

The json format is usually used for work. Introduction to JSON. Creating and reading JSON format with PHP


) and between the servers themselves (HTTP software interfaces). JSON is also well suited for storing complex dynamic structures in relational databases or file caches.

Since the JSON format is a subset of the syntax JavaScript language then it can be quickly deserialized with the built-in eval () function. In addition, it is possible to insert fully functional JavaScript functions. In PHP 5.2.0 and later, JSON support is included in the core in the form of json_decode () and json_encode () functions, which themselves convert JSON data types to their corresponding PHP types and vice versa.

Syntax

JSON is built on two structures:

  • A set of key / value pairs. In various languages ​​this is implemented as an object, record, structure, dictionary, hash table, keyed list, or associative array. The key can only be a string, the value can be any form.
  • A numbered set of values. In many languages ​​this is implemented as array, vector, list or sequence.

These are generic data structures. In theory, everything modern languages programming support them in one form or another. Since JSON is used to exchange data between different programming languages, it makes sense to build it on these structures.

JSON uses the following forms:

  • An object is an unordered set of name / value pairs enclosed in curly braces (). There is a symbol between the name and the meaning ": " and the name / value pairs are separated by commas.
  • Array(one-dimensional) is a set of values ​​that have ordinal numbers (indices). The array is contained in square brackets... The values ​​are separated by commas.
  • Meaning may be string in double quotes, number, value true or false, object, array, or the value null... These structures can be nested within each other.
  • Line is an ordered set of zero or more Unicode characters, enclosed in double quotes, using backslash escape sequences. Characters are represented by a simple string.
  • Name is a string.

Line very similar to string in languages ​​and Java. Number is also very similar to a C or Java number, except that only decimal format is used. Spaces can be inserted between any two characters.

The following example shows the JSON representation of an object describing a person. The object has string first and last name fields, an object describing the address, and an array containing a list of phone numbers.

("firstName": "Ivan", "lastName": "Ivanov", "address": ("streetAddress": "Moskovskoe sh., 101, apt. 101", "city": "Leningrad", "postalCode": 101101), "phoneNumbers": ["812 123-1234", "916 123-4567"])

In XML language, such a structure would look something like this:

> > Ivan > > Ivanov > > > Moskovskoe sh., 101, apt. 101 > > Leningrad > > 101101> > > > 812 123-1234> > 916 123-4567> > >

"Moskovskoe sh., 101, apt. 101" city ​​= "Leningrad" postalCode = "101101" /> > > 812 123-1234> > 916 123-4567> > >

Comparison with YAML

Both functionally and syntactically, JSON is a subset of the YAML language. In particular, the YAML 1.2 specification states that "any JSON file is a valid YAML file." The most common YAML parser is capable of handling JSON as well. The YAML spec prior to version 1.2 did not fully cover JSON, primarily due to the lack of native UTF-32 support in YAML, and the requirement for a space after the comma separator. In addition, the JSON specification included / * * / style comments.

The most important YAML difference is a set of syntax extensions that have no counterpart in JSON:

Relational: YAML supports relational data: in a YAML document, you can refer to an anchor previously encountered in a file / stream. This is how recursive structures can be expressed. Extensible: YAML supports extensible data types besides primitives (i.e. strings, numbers, booleans). Blocks: Indented block syntax is available in YAML; it allows you to describe structured data without using extra characters (all kinds of brackets, quotes, etc.).

JSON Schema

JSON Schema is one of the languages ​​for describing the structure of a JSON document. Uses JSON syntax. Based on the concepts of XML Schema, RelaxNG, Kwalify. JSON Schema is a self-describing language: when used to process data and describe its validity, the same serialization / deserialization tools can be used.

Using JSON in Ajax

The following Javascript code example shows how the browser can use XMLHttpRequest to request a JSON object from the server (the server side of the program is omitted; it must contain the code that sends data in the JSON string format in response to url requests).

Var the_object; var http_request = new XMLHttpRequest (); http_request.open ("GET", url, true); http_request.send (null); http_request.onreadystatechange = function () (if (http_request.readyState == 4) (if (http_request.status == 200) (the_object = JSON.parse (http_request.responseText);) else (alert ( "There was a problem with the URL."); ) http_request = null; ));

notice, that given example XMLHttpRequest is not universal for all browsers (for browsers based on Internet Explorer, Opera, Safari, and Mozilla, there must be some differences in the code). XMLHttpRequest is limited by the same origin policy: the response URL must be in the same DNS domain as the server hosting the page requesting the response. Alternatively, a JSONP approach is taken that involves using a coded function call between the client and the server so that the client can load JSON-encoded data from third-party domains and notify the caller of completion, although this introduces some security risks and additional server requirements.

Alternatively, in the page code, you can use the elements