I have written a javascript function to show all the inner data elements of a JSON object in an alert.
Here is my code
function allElements(moduleName) {
switch (moduleName) {
default :
return {
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language,such as DocBook",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
}
}
function getData(data) {
var table = ""
for (var i in data) {
if (i.constructor === "string".constructor) {
table += i + " -> " + data[i]
} else if (i.constructor === [].constructor) {
for (j in i) {
table += getData(i[j])
}
} else if (i.constructor === {}.constructor) {
for (j in i) {
table += getData(i[j])
}
}
}
return table
}
function fun() {
alert(getData(allElements("ignored argument")))
}
above logic was here. But it shows only s1,s2,s3 and 'second object' only. The whole object is recognized only as one object. It does not go into that inner object. Can someone tell me how can I get this done.
(Edited the json object)