JSON data parsing and Java Object conversion and its use in jquery __python

Source: Internet
Author: User
Tags javascript eval
JSON Basics Understand

Json:javascript Object Notation (JavaScript object notation).

JSON is the syntax for storing and exchanging text information . like XML.

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

Examples to be used in each chapter

{"Employees": [{"FirstName": "Bill", "LastName": "Gates"}, {"FirstName": "George", "LastName": "Bush"}, {"FirstName": "Thomas", "LastName": "Carter"}]}

This employee object is an array that contains 3 employee records (objects).

What is JSON. JSON refers to the JavaScript object notation (JAvaScript Object Notation) JSON is a lightweight text data interchange Format JSON is independent of language * JSON is self-describing 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 the same as the code that creates the JavaScript object.

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


Try it for yourself-an example

With our editor, you can edit the JavaScript code online and view the results by clicking a button:

Give it a shot yourself.

Like XML JSON is plain text JSON has "self-describing" (human-readable) JSON has a hierarchy (value exists) JSON can parse JSON data via JavaScript and can be transmitted using AJAX

Compared to the difference in XML there is no end tag shorter read and write faster able to use the built-in JavaScript eval () method to parse using arrays do not use reserved words

Why use JSON.

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

Reading XML documents using XML uses the XML DOM to iterate through the document read values and store them in a variable

Using JSON to read JSON strings processing JSON strings with eval ()

the JSON syntax is a subset of the JavaScript syntax.

JSON Syntax rules

The JSON syntax is a subset of the JavaScript object representation syntax. Data in name/value pairs data by comma-delimited curly braces save an array with an object square bracket

JSON Name/value pair

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

A name/value pair includes a field name (in double quotes), followed by a colon, followed by a value:

"FirstName": "John"

This is easy to understand, equivalent to this JavaScript statement:

FirstName = "John"

JSON value

The JSON value can be: numeric (integer or floating-point) string (in double quotes) a logical value (TRUE or FALSE) an 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" lastName = "Doe"

JSON Array

The JSON array is written in square brackets:

Arrays can contain multiple objects:

{"Employees": [{"FirstName": "John", "LastName": "Doe"}, {"FirstName": "Anna", "LastName": "Smith"}, {"FirstName": "Pe Ter "," lastName ":" Jones "}]}

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

JSON uses JavaScript syntax

Because JSON uses JavaScript syntax, JSON can be processed in JavaScript without additional software.

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

Example

var employees = [{"FirstName": "Bill", "LastName": "Gates"}, {"FirstName": "George", "LastName": "Bush"}, {"FirstName" : "Thomas", "LastName": "Carter"}];

You can access the first item in a JavaScript object array like this:

Employees[0].lastname;

The returned contents are:

Gates

You can modify the data like this:

Employees[0].lastname = "Jobs";

Give it a shot yourself.

In the following sections, you will learn how to convert JSON text to JavaScript objects.

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




Convert JSON text to JavaScript objects

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

To make it easier for you to explain, we use a string as input to demonstrate (rather than a file).

JSON instance-an object from a string

To create a JavaScript string that contains the JSON syntax:

var txt = ' {employees ': [' + ' {' firstName ']: "Bill", "LastName": "Gates"}, ' + ' {"FirstName ':" George "," LastName ":" Bus H "}, ' + ' {" FirstName ":" Thomas "," LastName ":" Carter "}]} ';

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

The eval () function uses the JavaScript compiler, which parses JSON text and then generates a JavaScript object. You must enclose the text in parentheses to avoid grammatical errors:

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

To use a JavaScript object in a Web page:

Example

<p> Name: <span id= "fname" ></span><br/> Last Name: <span id= "lname" ></span>& lt;br/> </p> <script type= "Text/javascript" > document.getElementById ("fname"). InnerHTML = Obj.employees[1].firstname document.getElementById ("lname"). InnerHTML = Obj.employees[1].lastname </script>

Give it a shot yourself.

JSON Parser

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

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

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

Native support for JSON 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 Safari 4 JQuery Yahoo UI Prototype Dojo ECMAScript 1.5

Give it a shot yourself.

For older browsers, you can use the JavaScript library: Https://github.com/douglascrockford/JSON-js

The JSON format was originally developed by Douglas Crockford.

jquery methods for parsing JSON data and 3 ways to convert strings into JSON

The method of parsing JSON data in jquery, as the transmission object of the jquery asynchronous request, the result returned by the jquery request is the JSON object, which takes into account the form of a string returned in JSON form by the server. The same is the case with JSON objects encapsulated by plug-ins such as Jsonobject, which are no longer described here. &NBSP
This first gives the set of JSON strings, which are set as follows: &NBSP
Code as follows: &NBSP
var data= " 

root: 

{name: ' 1 ', Value: ' 0 '}, 
{name: ' 6101 ', Value: ' Beijing '}, 
{name: ' 6102 ', Value: ' Tianjin '}, 

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.