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:
//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):
<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:
// The following is the operation of adding, deleting, checking and modifying json objects

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

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
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
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)