I am developing a web application in javascript (both on the server and client side). I am sending back and forth data as json, and I want to be able to parse it on the other side. The problem is that I have several levels of nested objects inside, so this is where I am stuck. For example, I am sending the following data:
var data = {};
data.title = "My Title";
data.metric = {
fact : "Malicious code detected",
technique : "XSS"
};
data.subject = {
userType : "ADMIN",
userName : "Jack",
clientNumber : "000",
terminal : "192.168.1.1"
};
data.context = {
environment : {
session : "00",
hostname : "mainServer",
sysType : "production"
},
resource : {
wpt : "DIA",
pid : "1024"
}
};
On the other side, when I receive it, I just want to be able to completely loop through this object, and print its contents. I have seen a lot of similar questions on stackoverflow, but none of them have been helpful. Here is what I have done so far:
function display(data) {
var resp = "";
var prop = null;
var dataJSON = JSON.parse(data);
for (prop in dataJSON) {
if (patternJSON.hasOwnProperty(prop)) {
resp += "obj" + "." + prop + " = " + dataJSON[prop] + "\n";
}
}
return resp;
}
But I do not know how to make it automatically go deeper, no matter the number of levels. Can somebody point me to the right direction please? Any help would be greatly appreciated! 10x