Json2.js processing JavaScript JSON data __JS

Source: Internet
Author: User

Reprint: HTTP://HI.BAIDU.COM/OEMGHCXTFHBAINQ/ITEM/D6F18EDBBA4F7211E1F46F6C

the experience of using methods of JSON2

The experience of using methods of JSON2


var myjsonobject = {"Bindings": [
{"Ircevent": "Privmsg", "Method": "Newuri", "regex": "^http://.*"},
{"Ircevent": "Privmsg", "Method": "Deleteuri", "regex": "^delete.*"},
{"Ircevent": "Privmsg", "Method": "Randomuri", "regex": "^random.*"}
]
};

In this example, an object is created that contains only one member, "bindings". "Bindings" is an array that contains 3 objects, each of which has 3 members, "Ircevent", "Method", and "regex".

These members can use the "." or subscript operation is obtained.

such as: Myjsonobject.bindings[0].method//"Newuri"
Myjsonobject.bindings[1].deleteuri//"Newuri"

Declare a string to compare the difference between JSON text and our normal text
var normalstring= ' [{persons:[{name: "Jordan", Sex: "M", Age: "{"}, {name: "Bryant", Sex: "M", Age: "A"}, {name: "McGrady", Sex: "M", Age: "27"}]}] ';
var jsontext= ' [{persons ']: [{' Name ': ' Jordan ', ' sex ': ' m ', ' Age ': '} ', {' name ': ' Bryant ', ' sex ': ' m ', ' Age ': ' '}, {' Name ":" McGrady "," Sex ":" M "," Age ":" 27 "}]}] ';

We can use the Eval () function to invoke the JavaScript compiler to turn JSON text into objects. Because JSON is an exact subset of JavaScript, the compiler can parse the JSON text correctly and then generate an object structure.

Call the Eval function into a JSON object,
var Mye = eval (normalstring);

Convert a JSON object to a string
var text = json.stringify (Mye);

Compare the converted JSON text to the declared text difference
Document.writeln (' converted JSON text: ' +text+ ' <br><br> declared JSON-formatted text ' +jsontext+ ' <br><br> declared plain text ' +normalstring+ ' <br><br> ');

The results are as follows:

Converted JSON text: [{persons]: [{' Name ': ' Jordan ', ' sex ': ' m ', ' age ': '},{' ' name ': ' Bryant ', ' sex ': ' m ', ' Age ': ' ' '},{' name ': "McGrady", "Sex": "M", "Age": "27"}]}

Declared JSON-formatted text [{persons]: [{' Name ': ' Jordan ', ' sex ': ' m ', ' age ': '},{' ' name ': ' Bryant ', ' sex ': ' m ', ' Age ': ' ' '},{' name ': "McGrady", "Sex": "M", "Age": "27"}]}

Plain format text declared [{persons:[{name: "Jordan", Sex: "M", Age: "{"}, {name: "Bryant", Sex: "M", Age: "A"}, {name: "McGrady", Sex: "M", Age: "27"}]}]

Summary: The converted JSON text and the declared JSON-formatted text content are the same.

It's better to use JSON parsing when security is more important. JSON parsing only recognizes JSON text and it is more secure, and the following call JSON's parse function generates a JSON data structure for text conversion
var myData = Json.parse (Jsontext);

The complete file is as follows (difference: myjsonobject,jsontext,normalstring difference):
<%@ page language= "java" pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<script type= "Text/javascript" src= "Js/json2.js" ></script>

<body>
<script>
var normalstring= ' [{persons:[{name: "Jordan", Sex: "M", Age: "{"}, {name: "Bryant", Sex: "M", Age: "A"}, {name: "McGrady", Sex: "M", Age: "27"}]}] ';
var jsontext= ' [{persons ']: [{' Name ': ' Jordan ', ' sex ': ' m ', ' Age ': '} ', {' name ': ' Bryant ', ' sex ': ' m ', ' Age ': ' '}, {' Name ":" McGrady "," Sex ":" M "," Age ":" 27 "}]}] ';
var myjsonobject = {"Bindings": [
{"Ircevent": "Privmsg", "Method": "Newuri", "regex": "^http://.*"},
{"Ircevent": "Privmsg", "Method": "Deleteuri", "regex": "^delete.*"},
{"Ircevent": "Privmsg", "Method": "Randomuri", "regex": "^random.*"}
]
};

Call the Eval function into a JSON object,
var Mye = eval (normalstring);

Convert a JSON object to a string
var text = json.stringify (Mye);

Compare the converted JSON text to the declared text difference
Document.writeln (' converted JSON text: ' +text+ ' <br><br> declared JSON-formatted text ' +jsontext+ ' <br><br> declared plain text ' +normalstring+ ' <br><br> ');

JSON parsing
var myData = Json.parse (Jsontext);
</script>
</body>


4.) Sample Demo II:

The following is a check on the JSON object's additions and deletions
<%@ page language= "java" pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<script type= "Text/javascript" src= "Js/json2.js" ></script>

<body>
<script>
Declaring a JSON object
var jsonobj2={persons:[
{Name: "Jordan", Sex: "M", Age: "40"},
{Name: "Bryant", Sex: "M", Age: "28"},
{Name: "McGrady", Sex: "M", Age: "27"}
]};
var persons=jsonobj2.persons;
var str= "";
var person={name: "Yaoming", Sex: "M", Age: "26"};

The following is the operation of the JSON object, which removes the annotation to view the results of the operation
JsonObj2.persons.push (person);//array last add a record
JsonObj2.persons.pop ()//Delete last item
JsonObj2.persons.shift ();//Delete first item
JsonObj2.persons.unshift (person);//array at the front of a record as long as the appropriate JavaScript method can be used in an array of JSON objects. So there is another way to splice () CRUD operations. Delete
JsonObj2.persons.splice (0,2)//start position, delete number

Replace do not delete
var self={name: "Tom", Sex: "M", Age: "24"};
var brother={name: "Mike", Sex: "M", Age: "29"};
JsonObj2.persons.splice (1,0,self,brother,self);//start position, delete number, insert Object

Replace and delete
JsonObj2.persons.splice (0,1,self,brother);//start position, delete number, insert Object

for (Var i=0;i<persons.length;i++) {
var cur_person=persons[i];
str+=cur_person.name+ "' Sex is" +cur_person.sex+ "and" the "is" +cur_person.age+ "<br><br>";
}
Document.writeln (str);
Convert to JSON text
var myjsonobj = json.stringify (JSONOBJ2);
Document.writeln (Myjsonobj);

Document.writeln (persons.length);

</script>
</body>

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.