Home Web Front-end JS Tutorial Preliminary learning and understanding of json2.js_json

Preliminary learning and understanding of json2.js_json

May 16, 2016 pm 06:01 PM

Initial learning and understanding of json2.js
1.) The download address of the js is: http://www.json.org/json2.js
2.) Quote the script on the page:
3.) Example Demonstration 1:

Copy code The code is as follows:

//Declare the json data structure directly
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 containing 3 objects, and each object has 3 members: "ircEvent", "method" and "regex".
These members can be obtained using "." or subscript operations.
For example: myJSONObject.bindings[0].method // "newURI"
myJSONObject.bindings[1].deleteURI // "newURI"
//Declaration string, you can compare the json text with ours The difference between normal text
var normalstring='[{persons:[{name:"jordan",sex:"m",age:"40"}, {name:"bryant",sex:"m",age :"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]';
var jsontext='[{"persons":[{"name":" jordan","sex":"m","age":"40"}, {"name":"bryant","sex":"m","age":"28"}, {"name" :"McGrady","sex":"m","age":"27"} ]}]';
We can use the eval() function to call the JavaScript compiler to convert JSON text into an object. Because JSON is an exact subset of JavaScript, the compiler can correctly parse the JSON text and then generate an object structure.
//Call the eval function to convert to a json object,
var myE = eval(normalstring);
//Convert the json object to a string
var text = JSON.stringify(myE);
//Compare the difference between the converted json text and the declared text
document.writeln('Converted json text:' text '

Declared json format text' jsontext '< br>
Declared normal format text 'normalstring '

');
The result is as follows:
Converted json text: [{"persons":[{"name ":"jordan","sex":"m","age":"40"},{"name":"bryant","sex":"m","age":"28"},{ "name":"McGrady","sex":"m","age":"27"}]}]
Declared json format text [{"persons":[{"name":"jordan" ,"sex":"m","age":"40"},{"name":"bryant","sex":"m","age":"28"},{"name":" McGrady","sex":"m","age":"27"}]}]
The plain text of the declaration [{persons:[{name:"jordan",sex:"m",age: "40"}, {name:"bryant",sex:"m",age:"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]
Summary: The converted json text and the declared json format text content are the same.
//When security is more important, it is better to use JSON parsing. JSON parsing will only recognize JSON text and it is more secure. The parse function of json is called below to convert the text data to generate a json data structure
var myData = JSON.parse(jsontext);
The complete file is as follows (difference: myJSONObject , jsontext, normalstring):
Copy code The code is as follows:

<%@ page language="java" pageEncoding="UTF-8"%>






<script> <br>var normalstring='[{persons:[{name:"jordan",sex:"m",age:"40"}, {name:"bryant",sex:"m ",age:"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]'; <br>var jsontext='[{"persons":[{"name ":"jordan","sex":"m","age":"40"}, {"name":"bryant","sex":"m","age":"28"}, { "name":"McGrady","sex":"m","age":"27"} ]}]'; <br>var myJSONObject = {"bindings": [ <br>{"ircEvent": " PRIVMSG", "method": "newURI", "regex": "^http://.*"}, <br>{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, <br>{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} <br>] <br>}; <br>//Call the eval function to convert to a json object, <br>var myE = eval(normalstring); <br>//Convert the json object to a string <br>var text = JSON.stringify(myE); <br> //Compare the difference between the converted json text and the declared text<br>document.writeln('Converted json text:' text '<br><br>Declared json format text' jsontext '<br> <br>Declared normal string 'normalstring '<br><br>'); <br>//JSON parsing<br>var myData = JSON.parse(jsontext); <br></script&gt ; <BR></body> <br></html> <br> </div> <br>4.) Example demonstration two: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="4274" class="copybut" id="copybut4274" onclick="doCopy('code4274')"><u>Copy code </u></a></span> The code is as follows: </div> <div class="codebody" id="code4274"> <br>// The following is the operation of adding, deleting, checking and modifying json objects <br><%@ page language="java" pageEncoding="UTF-8"%> <br><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <br><html> <br><head> <br><script type="text/javascript" src="js/json2.js"></ script> <br></head> <br><body> <br><script> <br>//Declare json object<br>var jsonObj2={persons:[ <br>{name:"jordan ",sex:"m",age:"40"}, <br>{name:"bryant",sex:"m",age:"28"}, <br>{name:"McGrady",sex: "m",age:"27"} <br>]}; <br>var persons=jsonObj2.persons; <br>var str=""; <br>var person={name:"yaoMing",sex: "m",age:"26"}; <br>//The following is the operation of the json object, remove the comment to view the operation result<br>jsonObj2.persons.push(person);//Add a record at the end of the array<br>jsonObj2.persons.pop();//Delete the last item<br>jsonObj2.persons.shift();//Delete the first item<br>jsonObj2.persons.unshift(person);//Add to the front of the array A record can be used in an array of JSON objects as long as it is suitable for Javascript! So there is another method splice() for crud operation! //Delete<br>jsonObj2.persons.splice(0,2);//Start position, delete number<br>//Replace without deleting<br>var self={name:"tom",sex:"m ",age:"24"}; <br>var brother={name:"Mike",sex:"m",age:"29"}; <br>jsonObj2.persons.splice(1,0,self, brother, self);//Starting position, number of deletions, insert object<br>//Replace and delete<br>jsonObj2.persons.splice(0,1,self,brother);//Start position, number of deletions , insert object <br>for(var i=0;i<persons.length;i ){ <br>var cur_person=persons[i]; <br>str =cur_person.name "'sex is " cur_person.sex " and age is " cur_person.age "<br><br>"; <br>} <br>document.writeln(str); <br>//Convert to json text<br>var myjsonobj = JSON.stringify (jsonObj2); <br>document.writeln(myjsonobj); <br>document.writeln(persons.length); <br></script>


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 jQuery Fun and Games Plugins 10 jQuery Fun and Games Plugins Mar 08, 2025 am 12:42 AM

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

jQuery Parallax Tutorial - Animated Header Background jQuery Parallax Tutorial - Animated Header Background Mar 08, 2025 am 12:39 AM

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Auto Refresh Div Content Using jQuery and AJAX Auto Refresh Div Content Using jQuery and AJAX Mar 08, 2025 am 12:58 AM

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

Getting Started With Matter.js: Introduction Getting Started With Matter.js: Introduction Mar 08, 2025 am 12:53 AM

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

See all articles