JSON and JavaScript objects

Source: Internet
Author: User
Tags comparison table

var obj={width:100,height:200}, which is not called JSON, and JSON is just a data format, not a concrete instance.

But many people think of such a JS object as JSON, the following to clarify the problem

First, JSON What is it? ( What is the difference between JSON and JavaScript objects?) How do i convert a JS object to a JSON string? And how to convert a JSON string into a JavaScript object?

JSON (JavaScript Object Notation) is a lightweight data interchange format , JSON-formatted data, primarily for cross-platform exchange of data.

But JSON and JavaScript do have roots, which can be said to evolve from JavaScript objects, which are a subset of JavaScript. The JSON itself means JavaScript Object Notation (Notation), which uses strict JavaScript Object notation to represent structured data.

It is a strict format of the JS object, the JSON property name must have double quotation marks, if the value is a string, also must be double quotation marks;

JSON is just a data format (or data form), the data format is actually a specification, format, form, specification can not be used to save data. We cannot call the following objects JSON, for example:

<script>

var obj2={};//this is just a JS object

var obj3={width:100,height:200};/* this is not the same as JSON, just JS Object */

var obj5={"width": +, "height": $, "name": "Rose"}; /* We can call this: a JSON-formatted JavaScript Object */

var str1= ' {' width ': $, ' height ': $, ' name ': ' Rose '} ';/* We can call this: a JSON-formatted string */

var a=[

{"width": +, "height": $, "name": "Rose"},

{"width": +, "height": $, "name": "Rose"},

{"width": +, "height": $, "name": "Rose"},

];

/* This is a JSON-formatted array, which is a slightly more complex form of JSON */

var str2= ' [' +

' {' width ': +, ' height ': $, ' name ': ' Rose '}, ' +

' {' width ': +, ' height ': $, ' name ': ' Rose '}, ' +

' {' width ': +, ' height ': $, ' name ': ' Rose '}, ' +

'] ' ;

/* This is called a slightly more complex JSON-formatted string */

</script>

JSON and JS object comparison table

Difference

Json

Javascript Object

Meaning

is just a data format

Represents an instance of a class

Transmission

Can transfer data across platforms with fast speed

Cannot transmit

Performance

1, key value pair mode, key must be double quotation mark

2, the value cannot be a method function, not a Undefined/nan

1, key value pair mode, key without quotation marks

2, values can be functions, objects, strings, numbers, Boolean, and so on

Convert each other

Convert JSON to JS object:

1,

Json.parse (jsonstring);

(not compatible with IE7)

2,

Jsobj=eval ("(" +jsonstring+ ")");

(Compatible with all browsers, but not secure, will execute the JSON inside the expression?)

The JS object is converted to JSON:

Json.stringify (jsobj);(incompatible IE7)

Other

Invoke the JSON website JS to implement the parse and stringify compatibility in each browser

: Https://github.com/douglascrockford/JSON-js/blob/master/json2.js

JSON and JavaScript objects

Related Article

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.