A problem caused by the non-uniform specification of JS JSON and string conversion process

Source: Internet
Author: User
Tags parse error javascript eval

For a two string:

String 1:{title:{},tooltip:{trigger: "axis"},legend:{data:["new followers"]},calculable:true,xaxis:[{type: category, boundarygap:false,data:["2016-01-01", "2016-01-02", "2016-01-03", "2016-01-04", "2016-01-05", "2016-01-06", " 2016-01-07 "]}],yaxis:{},series:[{name: New number of followers", type: "line", data:[11,11,15,13,12,13,10]}]}

String 2:

{"title": {}, "tooltip": {"trigger": "Axis"}, "Legend": {"Data": ["new number of followers"]}, "calculable": True, "Xaxis": [{"Type": " Category "," Boundarygap ": false," data ": [" 2016-01-01 "," 2016-01-02 "," 2016-01-03 "," 2016-01-04 "," 2016-01-05 "," 2016-01-06 "," 2016-01-07 "]}]," YAxis ": {}," series ": [{" Name ":" New number of followers "," type ":" Line "," Data ": [11,11,15,13,12,13,10]}] }

Can you see what the difference is between two JSON?

In JS, directly when the JSON data used, can normally parse out the JSON data.

But the string 1, when the format is validated in http://www.kjson.com/,

Parse error on line 1:{    title: {          -----^expecting ' STRING ', '} '

String 2 is normal.

When put in JS, string 1 and string as strings into JSON format data, string 1 error, prompt, and string can be converted to JSON format data.

What is the reason, as if it does not seem to be too much, for what reason?

In essence, you have to look at the syntax definition of JSON.

The writing format for JSON data is: name/value pairs.

name/value pairs include the field name (in double quotation marks), followed by a colon, and then the value:
"FirstName": "John"

Important thing to say three times: name/value pairs include field names (in double quotes), be sure to remember the most in double quotes

Knowledge Extension:


Json:javascript Object Notation (JavaScript object Notation).
JSON is the syntax for storing and exchanging textual information. Similar to XML.
JSON is smaller, faster, and easier to parse than XML.


Like 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


Compared to the difference between 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


Why use JSON?
For AJAX applications, JSON is faster and easier to use than XML:
Using XML
Reading an XML document
Using the XML DOM to iterate through a document
Read the value and store it in a variable
Using JSON
Reading JSON strings
Working with the JSON string with eval ()

A problem caused by the non-uniform specification of JS JSON and string conversion process

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.