search
HomeWeb Front-endJS TutorialPreliminary learning and understanding of json2.js_json

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 '
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:








<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>> ;



4.) Example demonstration two:
Copy code The code is as follows:

// The following is the operation of adding, deleting, checking and modifying json objects




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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)