I have a variable depth JSON document. Example:
[ { "type": "firsttype", "text": { "id": "content" } } ]
What I'm trying to do is get the values ββof certain keys, for example text . Since I do not know where these keys can appear in .JSON, I need to use a recursive function. Then I try to display these keys and values ββin an HTML file.
I have a preliminary attempt:
$.getJSON("file.json", function getText (oValue, sKey) { links = []; if (typeof oValue == "object" || typeof oValue == "array") { for (i in oValue) { getText (oValue [i], i); } } else { links.push ( "<li id='" + oValue + "'>" + sKey + "</li>" ); } $( "<ul/>", { "class": "my-new-list", html: links.join( "" ) }).appendTo( "body" ); });
When I load a page locally or remotely or on python -m SimpleHTTPServer , I get no errors and nothing on the page. What am I doing wrong? I have included all JS in the $.getJSON call so that there are no problems with asynchronism.
In the future, I would also like to include regular expression checking so that I can retrieve values ββwith a specific string, for example. /http/ . What would be the best way to do this?
source share