Introduction to JSON

Source: https://javaee.github.io/tutorial/jsonp001.html

Introduction to JSON

JSON is a text-based data interchange format, derived from JavaScript, used for web services and other connected applications.

 

JSON syntax

JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, an array is a list of values. JSON defines seven value types: strings, numbers, objects, arrays, true, false, and null values.

JSON has the following syntax.

  • Objects are enclosed in curly braces ( {}), their name-value pairs are separated by commas ( ,), and the name and value within a pair are :separated by colons ( ). Names in objects are strings, while values ​​can be any of seven value types, including another object or an array.

  • Arrays are enclosed in square brackets ( []), and their values ​​are separated by commas ( ,). Each value in the array can be of a different type, including another array or object.

  • When objects and arrays contain other objects or arrays, the data has a tree structure.

  • Use of JSON

    JSON is often used as a common format for serializing and deserializing data in applications that communicate with each other over the Internet. These applications are created in different programming languages ​​and run in very different environments. JSON is suitable for this because it is an open standard, it is easy to read and write, and it is more compact than other representations.

    RESTful web services use JSON extensively as the data format in requests and responses. HTTP headers are used to indicate that the content of the request or response is JSON data

    Content-Type: application/json

    JSON representations are generally more compact than XML representations because JSON has no closing tags. Unlike XML, JSON does not have a widely accepted schema for defining and validating the structure of JSON data.

    Generate and parse JSON data

    For generating and parsing JSON data, there are two programming models that are similar to those used for XML documents.

    • The object model creates a tree representing JSON data in memory. The tree can then be navigated, analyzed or modified. This approach is the most flexible and allows processing that requires access to the full contents of the tree. However, it is generally slower than streaming mode and requires more memory. The object model generates JSON output by navigating the entire tree at once.

    • The streaming model uses an event-based parser that reads JSON data one element at a time. The parser generates events and stops processing when it finds a key or when it finds a value at the beginning or end of an object or array. Each element can be processed or discarded by application code, and the parser then moves on to the next event. This approach is suitable for local processing, where processing elements do not require information from the rest of the data. The streaming model produces JSON output for a given stream by making function calls on one element at a time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769932&siteId=291194637