Json advantages and disadvantages and usage introduction

Json advantages and disadvantages and usage introduction

1. What is JSON

The concept of JSON is very simple. JSON is a lightweight data format based on a subset of JavaScript syntax, namely array and object representation. Since javascript syntax is used, JSON definitions can be included in javascript files and accessed without additional parsing through XML-based languages. But before using JSON, it is important to understand the special syntax of array and object literals in JavaScript.

1.1 Array literals

An array literal is a set of comma-separated JavaScript values ​​enclosed in square brackets, for example:

var aNames=["hello", 12, true , null];

1.2 Object literals

Object literals are defined by enclosing two curly braces. Any number of name-value pairs can be placed within the curly braces, defining the format string value. Except for the last line, each name-value pair must be followed by a comma (this is somewhat similar to the definition of an associative array in Perl). For example:

        var oCar = {

               "color": "red",       

              "doors" : 4,

               "paidFor" : true

        };

1.3 Mixed Literals

We can mix object and array literals to create an array of objects, or an object containing an array. For example:

{comments:[
{
id:1,
author:"someone1",
url:"http://someone1.x2design.net",
content:"hello"
},
{
id:2,
author:"someone2",
url:"http://someone2.x2design.net",
content:"hello"
},
{
id:3,
author:"someone3",
url:"http://someone3.x2design.net",
content:"hello"
}
]};

1.4 JSON Syntax

In Ajax applications, the server directly generates JavaScript statements, and the client directly uses the eval method to obtain the object, thus eliminating the performance loss of parsing XML. At the same time, the benefit of using JSON as a data format in JavaScript communication is that the value of the data can be obtained immediately, so the data contained therein can be accessed faster.

var oCarInfo = eval("(" + sJSON + ")");

Remember: curly braces are also a statement in JavaScript. The only way for the parser to know that the curly braces represent an object rather than a statement is to find the parentheses enclosing them (which are used to indicate that the code is an expression rather than a statement).

1.5 JSON encoding and decoding

As part of the JSON resource, Corockford developed a tool that enables direct decoding and encoding of JSON and Javascript objects. The source code of this tool can be downloaded from https://github.com/douglascrockford/JSON-js.

There are some inherent deficiencies with using eval() mentioned above: it is designed to evaluate any JavaScript code passed in, not just JSON. Therefore, it has great security risks when it comes to enterprise-level web application development. To solve this problem, you can use the JSON.parse() method, a parser that only converts JSON code into Javascript. For example:

var oObject = JSON.parse(sJSON);

It also provides a tool for converting Javascript objects to JSON strings (used for data transmission) (there is no built-in support for this in Javascript). All you have to do is pass the object into the JSON.Stringify() method. Take a look at the following example:

       var oCar = new Object();

       oCar.doors = 4;

        oCar.color = "blue";

       oCar.year = 1995;

       oCar.drivers = new Array("Penny", "Dan", "Kris");

       document.write(JSON.stringify(oCar));

This code will output a JSON string like the following:

{"doors" : 4, "color" : "blue", "year" :1995, "drivers" : ["Penny", "Dan" , "Kris"]}

2. JSON vs XML

As mentioned above, one of the big advantages of JSON over XML is that it is much simpler.

See the XML data representation example:

Using XML representation:

<comments>
<comment>
<id>1</id>
<author>someone1</author>
<url>http://someone1.x2design.net</url>
<content>hello</content>
</comment>
<comment>
<id>2</id>
<author>someone2</author>
<url>http://someone2.x2design.net</url>
<content>someone1</content>
</comment>
<comment>
<id>3</id>
<author>someone3</author>
<url>http://someone3.x2design.net</url>
<content>hello</content>
</comment>
</comments>

Using JSON representation:

{comments:[
{
id:1,
author:"someone1",
url:"http://someone1.x2design.net",
content:"hello"
},
{
id:2,
author:"someone2",
url:"http://someone2.x2design.net",
content:"hello"
},
{
id:3,
author:"someone3",
url:"http://someone3.x2design.net",
content:"hello"
}
]};

It is easy to find that a lot of redundant information is missing. Since there is no need for a closing tag to match the opening tag, the number of bytes required to transmit the same information is greatly reduced. Founder Corockford calls it "XML's diet plan").

The disadvantage of JSON formatted data compared to XML is that it is less readable for laymen. Of course, there is a point of view that data exchange formats are not meant to be seen with the naked eye. If the data is being created and parsed back and forth by tools, there's really no reason that the data has to be human-readable. The bottom line is this: there are JSON tools available.

3. Server-side JSON tools

java: java JSON tool, developed by Douglas Crock ford, can be downloaded from https://github.com/stleary/JSON-java, which can be used in JSP.

4. JSON advantages and disadvantages

JSON not only reduces the performance and compatibility issues brought about by XML parsing, but is also very easy to use for JavaScript. It can conveniently obtain data by traversing arrays and accessing object properties. It is also readable and basically has the properties of structured data. I have to say it is a very good method, and in fact Google Maps does not use XML to transfer data, but uses JSON.

Another advantage of JSON is its cross-domain feasibility. For example, it is completely feasible to use it on a web page at www.xxx.com, which means you can transmit information across domains. However, using XMLHttpRequest cannot obtain cross-domain information, which is limited by the internal security nature of Javascript.

JSON looks beautiful, can it completely replace XML? This is not the case, and the reason lies in XML's strength: universality. It is not easy to make the server generate grammatically qualified JavaScript code. This mainly happens in larger systems where there are different developers on the server and client sides. They have to negotiate the format of the object, which can easily lead to errors.

JSON.parse(jsonstr); //You can convert json string into json object

JSON.stringify(jsonobj); //You can convert json objects into json strings

js to refresh this page: window.location.reload();

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of json file writing format
  • Detailed explanation of Json format
  • Understanding JSON (JavaScript Object Notation) in one article
  • JSON Introduction and Usage Summary
  • Detailed introduction to json objects in js
  • jQuery JSON parsing example
  • JSON principle analysis and example introduction
  • Introduction to JSON data format
  • Concise JSON Introduction
  • A brief introduction to json

<<:  Interactive experience trends that will become mainstream in 2015-2016

>>:  Solution to forgetting mysql database password

Recommend

How to create your first React page

Table of contents What is Rract? background React...

A brief discussion on JS packaging objects

Table of contents Overview definition Instance Me...

Introduction to the method attribute of the Form form in HTML

1 method is a property that specifies how data is ...

nginx solves the problem of slow image display and incomplete download

Written in front Recently, a reader told me that ...

Vue implements a simple timer component

When doing a project, it is inevitable to encount...

Detailed explanation of the execution plan explain command example in MySQL

Preface The explain command is the primary way to...

Analysis of Nginx Rewrite usage scenarios and configuration methods

Nginx Rewrite usage scenarios 1. URL address jump...

How to monitor Linux server status

We deal with Linux servers every day, especially ...

Solution to MySQL master-slave delay problem

Today we will look at why master-slave delay occu...

SASS Style Programming Guide for CSS

As more and more developers use SASS, we need to ...

Linux system (Centos6.5 and above) installation jdk tutorial analysis

Article Structure 1. Preparation 2. Install Java ...

HTML+Sass implements HambergurMenu (hamburger menu)

A few days ago, I watched a video of a foreign gu...

Six important selectors in CSS (remember them in three seconds)

From: https://blog.csdn.net/qq_44761243/article/d...

How to implement JavaScript's new operator yourself

Table of contents Constructor new Operator Implem...