Generate and parse JSON in JS

Source: Internet
Author: User

Generate JSON object in JS


Parse JSON string in JS:

1.eval ("..."):

var a_id=eval (' {' Name ': 123} ');

If the content in Eval contains malicious script, it will be troublesome.


2.json_parse:

You need to import Json_parse.js:

<script src= "Js/json_parse.js" ></script>
<script language= "JavaScript" >
var t = json_parse (' {' Name ': 123} ');
alert (t.name);
</script>>

Safe, fast parsing speed


3.var Vjson = Json.parse (JSONSTR):

Supports IE8 and FireFox3, two browsers with built-in JSON objects, and other browsers to introduce json.js files

<script src= "Js/json.js" ></script>
<script language= "JavaScript" >
var t = json.parse (' {' Name ': 123} ');
alert (t.name);
</script>
Security and resolution is fast.




----------------------------------Split Line-------------------------------------------------------

Parsing JSON strings in JS



json.stringify:

Grammar:
Json.stringify
(value [, Replacer] [, space])
Value: is the required field. Is the object you have entered, such as array Ah, class AH and so on.
Replacer: This is optional. It is divided into 2 ways, one is method, the second is an array.
Situation one: We first say the data, through the experiment behind us, we can know that it is related to the first one. In general, our serialization results are represented by key-value pairs.
For example:
Name: "LAN", age:25
This form.
So, if this is the case, if the value of the second exists in the first, then the value of the second is the key, the first value for value is represented, if it does not exist, sorry, ignore. "It's not a bit abstract, I think so, but you just wait and see the experiment is OK." Whirring 】
Situation Two: If it is a method, it is very simple, that is, the serialization of each object (remember that every one) into the method of processing.
Space: Well understood, with what to do the delimiter.
1. If omitted, then the displayed value does not have a separator character. Direct output to
2. If it is a number, then it is defined to indent a few characters, of course, if it is greater than 10, the maximum value is 10.
3. If there are some escape characters, such as "\ T", which means carriage return, then it is a carriage return per line.
4. If it is just a string, OK, append the strings to the output value in each row. Of course, the maximum length is also 10 characters.
begin to illustrate with examples;
1. In the case of only one parameter:

Copy the Code code as follows:


var student = new Object ();
Student.name = "Lanny";
Student.age = "25";
student.location = "China";
var json = json.stringify(student);
alert (student);


The results are as follows:

Some people might suspect json.stringify the role of OK. Well, if we don't want this function. The code looks like this:

Copy the Code code as follows:


var student = new Object ();
Student.name = "Lanny";
Student.age = "25";
student.location = "China";

var json = json.stringify(student);
alert (student);


Congratulations on the results you've got:

Don't lie to you, go on.
2. The second parameter is present, and the second argument is a function.

Copy the Code code as follows:


var students = new Array ();  
Students[0] = "Lanny";  
students[ 1] = "dong",  
students[2] = "I love You";  
var json = json.stringify (students,switchupper);  
function Switchupper (key, value) { 
return Value.tostring (). toUpperCase ();  

Alert (JSON); &NBSP

//var JSON = json.stringify (Students, function (key,value) { 

//return value.tostring (). toUpperCase ();   ;

//});  


The above method can also be replaced by the following, 2 are the same, but the wording of a little bit different.
The results were as follows:

3. The second parameter exists, and the second parameter is not a function, but an array.
3.1 "Misunderstanding" if the first parameter is an array, the second argument is an array, only the value of the first parameter is displayed.
Like what:

Copy the Code code as follows:


var students = new Array ();  
Students[0] = "Lanny";  
students[ 1] = "dong",  
students[2] = "I love You";  
var stu = new Array ();  
Stu[0] = "1";  
Stu[1] = "2";  
var json = json.stringify (students,stu);  
Alert (JSON);  


Sorry results are as follows:

The second one was ignored, but the first one was serialized.

3.2 If the first is an object (the object here is like a new in C #), the second one is an array.

Then if the value of the second exists in the first, then the second value is the key, and the first value is represented as value.

Copy the Code code as follows:


var student = new Object ();  
Student.qq =" 5485891512 ";  
Student.name = "Lanny";  
Student.age = 25; 

var stu = new Array ();  
Stu[0] = "QQ";  
STU[1] = "age";  
Stu[2] = "Hi";//This student object does not exist.  

var json = json.stringify (student,stu);  
Alert (JSON);  


The results are as follows:

Because stu[2] = "HI", this hi is not found in the first, so it is not displayed.

4. A third parameter

4.1. If omitted, then the displayed value does not have a separator character. Direct output to

Like what:

Copy the Code code as follows:


var student = new Object ();
STUDENT.QQ = "5485891512";
Student.name = "Lanny";
Student.age = 25;

var stu = new Array ();
Stu[0] = "QQ";
STU[1] = "age";
STU[2] = "Hi";

var json = json.stringify(student,stu);
alert (JSON);


The output is:

4.2. If it is a number, then it is defined to indent a few characters, of course, if it is greater than 10, the maximum value is 10.

Copy the Code code as follows:


var student = new Object ();  
Student.qq =" 5485891512 ";  
Student.name = "Lanny";  
Student.age = 25; 

var stu = new Array ();  
Stu[0] = "QQ";  
STU[1] = "age";  
Stu[2] = "Hi";  

var json = json.stringify (student,stu,100);//note here the 100 
Alert (JSON);  


Span style= "Color:rgb (51, 51, 51); > So what you get is:  

10 characters are empty.  



4.4. If it's just a string, OK, append the strings to each line of output values. Of course, the maximum length is also 10 characters.  

If it is var json =  json.stringify


Generate and parse JSON in JS

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.