The technique of _javascript and handling between JSON object and string in JS

Source: Internet
Author: User
Tags string to json

Json:javascript Object Notation (JavaScript Object notation), in fact, JSON is a JavaScript object.

If there is no clear json, you can go to w3cschool understand http://www.w3school.com.cn/json/

1. Create a new string (JSON text) in JavaScript.

var txt = ' {employees ': [' +
' {' firstName ': ' Bill ', ' lastName ': ' Gates '}, ' +
' {' firstName ': ' George ', ' Lastna Me ":" Bush "}, ' +

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:

 
 

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

String to JSON object: Json.parse (JSONSTR);

JSON Object goto JSON string: json.stringify (jsonobj);

jquery Method:

JSON string to JSON object: Jquery.parsejson (JSONSTR);

 
 

2. So how do I traverse the JSON array? You can treat it as an ordinary JavaScript object.

 
 

IE8 (compatibility mode), IE7 and IE6 do not have JSON objects, but http://www.json.org/js.html provides a json.js so IE8 (compatibility mode), IE7 and IE6 can support JSON objects and their stringify () and Parse () method; You can get this JS on the HTTPS://GITHUB.COM/DOUGLASCROCKFORD/JSON-JS, usually using json2.js now.

IE8 (compatible mode), IE7 and IE6 can use eval () to convert a string to a JSON object.

var c= ' {' name ': ' Mike ', ' sex ': ' Female ', ' age ': ' "} ';
var ctoobj=eval ("(" +c+ ")");

The above content is for JS in the JSON object and string between the transfer and processing skills, I hope to help. Next, let's add a little bit of knowledge to the conversion between string and JSON in JavaScript.

Here's an introduction to the standard browsers (such as Firefox, Chrome, etc.) that have an object JSON (note is uppercase) that is designed to handle JSON. This object has two functions:

Json.parse (text)//for parsing JSON;

Json.stringify (object)//for generating JSON format;

Earlier versions of IE did not have this object and could go to JSON's official website to download json2.js against earlier IE versions http://www.JSON.org

Here are some of the previous methods that you can now discard

1. The old method

function Strtojson (str) { 
var json = eval (' + str + ') '); 
return JSON; 
}

2. Commonly used

function Strtojson (str) {return 
(the new Function ("return" + str)) (); 

Methods not supported by 3.IE

function Strtojson (str) {return 
json.parse (str); 

4.jQuery method

Parsejson:function (data) { 
if (typeof data!== "string" | |!data) {return 
null; 
} 
data = Jquery.trim (data); 
if (/^[\],:{}\s]*$/.test (/\\ (?: ["\\\/bfnrt]|u[0-9a-fa-f]{4})/g," @ ") 
. Replace (/" [^ "\\\n\r]*" | true|false|null|-?\d+ (?: \. \d*)? (?: [ee][+\-]?\d+)?/g, "]") 
. Replace (/(?: ^|:|,) (?: \ s*\[) +/g, "")) {return 
window. JSON && windows. Json.parse? 
Window. Json.parse (data): 
(New Function ("return" + Data)) (); 
} else { 
jquery.error ("Invalid JSON:" + data); 
} 

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.