This is an example program that shows how to parse complex data types in a JSON string in JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>XoaX.net's Javascript</title> <!-- Supply a transparent 1x1 favicon --> <link rel="icon" href="data:,"> <script type="text/javascript" src="ParseComplexJson.js"></script> </head> <body onload="Initialize()"> <span id="idObject"></span> </body> </html>
function Initialize() {
const sJsonText = '{ "Name": "Jesus Christ", "Birth Date": "0000-12-25", "Description": "Son of God", "Lifespan": 33,'+
'"Disciples": ["John", "Peter", "Thomas", "James"], "Parents":{"Father":"Joseph","Mother": "Mary"} }';
const qJsonObject = JSON.parse(sJsonText);
qJsonObject["Birth Date"] = new Date(qJsonObject["Birth Date"]);
document.getElementById("idObject").innerHTML = PrintAnObject("qJsonObject", qJsonObject, sPre = "");
}
function PrintAnObject(sName, qObject, sPre = "") {
let sOutput = typeof(qObject) + " " + qObject.constructor.name + ": " + sName + '<br />';
for (let sProperty in qObject) {
if (typeof(qObject[sProperty]) == "object") {
sOutput += PrintAnObject(sProperty, qObject[sProperty], sPre + " ");
} else {
sOutput += sPre+" "+typeof(qObject[sProperty])+': '+sProperty+' = '+qObject[sProperty]+'<br />';
}
}
return sPre + sOutput;
}
© 20072026 XoaX.net LLC. All rights reserved.