我有一个可变深度的JSON文档.一个例子:
[
{
"type": "firsttype",
"text": {
"id": "content"
}
}
]
Run Code Online (Sandbox Code Playgroud)
我想要做的是检索某些键的值,比方说text.由于我不知道这些密钥在.JSON中可能出现在哪里,我需要使用递归函数.然后我尝试在HTML文件中显示这些键和值.
我在这里有一个初步的尝试:
$.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" );
});
Run Code Online (Sandbox Code Playgroud)
当我在本地或远程或加载页面时python -m SimpleHTTPServer …