小编Job*_*ong的帖子

我可以在$ .getJSON函数中使用递归JavaScript函数吗?

我有一个可变深度的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 …

javascript regex recursion jquery json

5
推荐指数
1
解决办法
290
查看次数

标签 统计

javascript ×1

jquery ×1

json ×1

recursion ×1

regex ×1