Home > Article > Backend Development > Summary of JSON and XML knowledge points
This article mainly shares with you a summary of js knowledge points. JSON objects are written in curly brackets ({}). An object can contain multiple key/value pairs. The key must be a string, and the value can be a legal JSON data type (string, number, object, array, boolean or null).
Use colon (:) to separate key and value.
Each key/value pair is separated by commas (,).
**eg:**var myjson={“name”:”zhangsan”, “age”:15};
Two ways to access object values:
myjson.name
myjson["name"]
for..in can be accessed iteratively Object
for(x in myjson) {
x is the name of the key in the object. When accessing the value here, you can only use the second method above
myjson[x]
}
Use delete to delete the attributes of the json object
delete myjson.name
delete myjson[“name”]
The difference between json object, json string and json array
var str2 = { “name”: “asan”, “sex” : "man" };//Object
var str1 = ‘{ “name”: “deyuyi”, “sex”: “man” }’;//String
var sites = [
{ “name”:”runoob” , “url”:”www.runoob.com” },
{ “name”:”google” , “url”:”www.google.com” },
{ “name”:”微博” , “url”:”www.weibo.com” }
];//Array
Array access: sites[0].name; return runoob
JSON.parse()
Convert the string into a JavaScript object.
JSON cannot store Date objects and needs to be converted into strings for storage
For details, please visit the URL
JSON.stringify()
Convert JavaScript objects into strings.
JSON cannot store Date objects.
JSON.stringify() will convert all dates to strings.
For details, please visit the website
XML tutorial for rookies
Parsing XML strings
txt =”“;
txt=txt+”Everyday Italian”;
txt=txt+”Giada De Laurentiis”;
txt=txt+”2005”;
txt=txt+””;
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,”text/xml”);
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=false;
xmlDoc.loadXML(txt);
}
Internet Explorer uses the loadXML() method to parse XML strings, while other browsers use the DOMParser object.
Parse XML document
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.open(“GET”,”books.xml”,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
The syntax for extracting text from an element is:
xmlDoc. getElementsByTagName(“to”)[0].childNodes[0].nodeValue;
The above is the detailed content of Summary of JSON and XML knowledge points. For more information, please follow other related articles on the PHP Chinese website!