JSON (original ecology)

Source: Internet
Author: User
Tags javascript eval

What is JSON?
    • JSON refers to the JavaScript object notation (JAvaScript Object Notation)
    • JSON is a lightweight text data interchange Format
    • JSON independent of Language *
    • JSON is self-descriptive and easier to understand
* JSON uses JavaScript syntax to describe data objects, but JSON is still independent of language and platform. The JSON parser and the JSON library support many different programming languages. Currently very much dynamic (php,jsp,. NET) programming language supports JSON.
JSON-Convert to JavaScript object

The JSON text format is syntactically identical to the code that creates the JavaScript object.

Because of this similarity, the JavaScript program can use the built-in eval () function to generate native JavaScript objects with JSON data without a parser.

Different from XML

    • No end tag
    • Even shorter
    • Read and write faster
    • Ability to parse using the built-in JavaScript eval () method
    • Working with arrays
    • Do not use reserved words

In the same place as XML
    • JSON is plain text
    • JSON has a "self-descriptive" (Human readable)
    • JSON has a hierarchy (values exist in values)
    • JSON can be parsed with JavaScript
    • JSON data can be transmitted using AJAX

JSON Syntax rules

JSON syntax is a subset of the JavaScript object notation syntax.

    • Data in name/value pairs
    • Data is separated by commas
    • Curly braces Save Object
    • Save array in brackets
Accessing object values

You can use the dot number (.) To access the value of the object:

InstanceVar myobjxmyobj = { "name" : "runoob" "alexa" : 10000 "site ":null } < Span class= "hl-quotes" > < Span class= "Hl-code" > < Span class= "hl-brackets" > x = myobj. Name

You can also use the brackets ([]) to access the value of the object:

InstanceVar MyObj,x; myobj = myobj = { "name" : " runoob " alexa ":10000 "site" :null } x = myObj["name"];

Exception parsing data

JSON cannot store Date objects.

If you need to store a Date object, you need to convert it to a string.

The string is then converted to a Date object.

Analytic functions

JSON does not allow a function to be included, but you can store the function as a string and then convert the string to a function.

JSON Parser

The eval () function compiles and executes any JavaScript code. This hides a potential security issue.

Using the JSON parser to convert JSON to JavaScript objects is a safer practice. The JSON parser only recognizes JSON text and does not compile the script.

In the browser, this provides native JSON support, and the JSON parser is faster.

Native JSON support is included in newer browsers and the latest ECMAScript (JavaScript) standards.

JSON compiler

Https://c.runoob.com/front-end/53

Web Compiler

Https://c.runoob.com/front-end/61

Original ecology

var dataobj=eval ("(" +data+ ")");//Convert to JSON object

Why to Eval here to add "("("+data+");//"?

The reason is that eval itself is a problem. Since JSON starts and ends in the form of "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.

The purpose of the parentheses is to force the Eval function to force the expression in parentheses to be converted to an object while processing the JavaScript code, rather than being executed as a statement (statement). For example, if the object literal {} is not enclosed, then eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty statement. So the following two execution results are different:
Alert (eval ("{}");//return undefined
Alert (eval ("({})");//return Object[object]

JSON (original ecology)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.