JavaScript attack (eight) JSON

Source: Internet
Author: User
Tags javascript eval



JSON is the syntax for storing and exchanging textual information. Similar to XML.



JSON is smaller, faster, and easier to parse than XML.


What is JSON?


JSON refers to JavaScript Object notation (JavaScript Object Notation), JSON is a lightweight text data Interchange Format, JSON is 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.


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.


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 through JavaScript, JSON data can be transmitted using AJAX


compared to the difference between XML


No end tag, shorter, faster read and write, ability to parse using the built-in JavaScript eval () method, use arrays, do not use reserved words


Why use JSON?


For AJAX applications, JSON is faster and easier to use than XML:


using XML


Read the XML document, use the XML DOM to iterate through the document, read the value, and store it in a variable


using JSON


Read the JSON string and process the JSON string with eval ()



JSON syntax is a subset of JavaScript syntax.


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 hold the object, square brackets Save the array


JSON name/value pairs


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"


This is easy to understand and is equivalent to this JAVASCRIPT statement:


FirstName = "John"
JSON Value


The JSON value can be: Number (integer or floating point), string (in double quotes), logical value (TRUE or FALSE), array (in square brackets), object (in curly braces), null


JSON Object


The JSON object is written in curly braces:



An object can contain multiple name/value pairs:


{"FirstName": "John", "LastName": "Doe"}


This is also easy to understand, and is equivalent to this JAVASCRIPT statement:


FirstName = "John"= "Doe"
JSON Array


The JSON array is written in square brackets:



An array can contain multiple objects:


 
 
1 {
2 "employees": [
3 { "firstName":"John" , "lastName":"Doe" },
4 { "firstName":"Anna" , "lastName":"Smith" },
5 { "firstName":"Peter" , "lastName":"Jones" }
6 ]
7 }


In the example above, the object "Employees" is an array that contains three objects. Each object represents a record of a person (with a first and last name).


JSON uses JavaScript syntax


Because JSON uses JavaScript syntax, there is no need for additional software to handle JSON in JavaScript.



With JavaScript, you can create an array of objects and assign values like this:


 
 
1 var employees = [
2 { "firstName":"Bill" , "lastName":"Gates" },
3 { "firstName":"George" , "lastName":"Bush" },
4 { "firstName":"Thomas" , "lastName": "Carter" }
5 ];


You can access the first item in an array of JavaScript objects like this:


Employees[0].lastname;


The returned content is:


Gates


You can modify the data like this:


Employees[0].lastname = "Jobs";
JSON File


The file type of the JSON file is ". JSON", and the MIME type of the JSON text is "Application/json"


convert JSON Text to JavaScript object


One of the most common uses of JSON is to read the JSON data (as a file or as a HttpRequest) from the Web server, convert the JSON data to a JavaScript object, and then use that data in the Web page.



To make it easier for you to explain, we use strings as input for demonstrations (not files).


JSON Instance-an object from a string


Create a JavaScript string that contains the JSON syntax:


 
var txt = ‘{ "employees" : [‘ +
‘{ "firstName":"Bill" , "lastName":"Gates" },‘ +
‘{ "firstName":"George" , "lastName":"Bush" },‘ +
‘{ "firstName":"Thomas" , "lastName":"Carter" } ]}‘;


Because JSON syntax is a subset of JavaScript syntax, the JavaScript function eval () can be used to convert JSON text to JavaScript objects.



The eval () function uses the JavaScript compiler to parse the JSON text and then generate the JavaScript object. You must enclose the text in parentheses in order to avoid syntax errors:


var obj = eval ("(" + txt + ")");


To use JavaScript objects in a Web page:


 
1 <p>
2 First Name: <span id="fname"></span><br />
3 Last Name: <span id="lname"></span><br />
4 </p>
5 
6 <script type="text/javascript">
7 document.getElementById("fname").innerHTML = obj.employees[1].firstName
8 document.getElementById("lname").innerHTML = obj.employees[1].lastName
9 </script>
JSON Parser


Tip: 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.


Web Browser Support Web Software Support
  • Firefox (Mozilla) 3.5
  • Internet Explorer 8
  • Chrome
  • Opera 10
  • Safari 4
  • Jquery
  • Yahoo UI
  • Prototype
  • Dojo
  • ECMAScript 1.5





JavaScript attack (eight) JSON


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.