我可以在$.getJSON函数中使用递归JavaScript函数吗?
我有一个深度可变的JSON文档。例如:我可以在$.getJSON函数中使用递归JavaScript函数吗?,javascript,jquery,json,regex,recursion,Javascript,Jquery,Json,Regex,Recursion,我有一个深度可变的JSON文档。例如: [ { "type": "firsttype", "text": { "id": "content" } } ] 我试图做的是检索某些键的值,比如text。因为我不知道这些键可能出现在.JSON中的什么位置,所以我需要使用递归函数。然后,我尝试在HTML文件中显示这些键和值 我在这里有一个初步尝试: $.getJSON("file.json", function ge
[
{
"type": "firsttype",
"text": {
"id": "content"
}
}
]
我试图做的是检索某些键的值,比如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" );
});
$.getJSON(“file.json”,函数getText(oValue,sKey){
链接=[];
if(typeof oValue==“object”| | typeof oValue==“array”){
对于(i为椭圆形){
getText(oValue[i],i);
}
}否则{
links.push(““+sKey+” ”;
}
$(“
”{
“类”:“我的新列表”,
html:links.join(“”)
}).附于(“主体”);
});
当我本地或远程加载页面时,或者在python-msimplehttpserver
上加载页面时,我没有收到任何错误,页面上也没有任何内容。我做错了什么?我已经在$.getJSON
调用中包含了所有的JS,因此不会出现异步问题
在将来,我还希望包含一个regexp检查,以便可以使用特定字符串提取值,例如
/http/
。最好的方法是什么?我认为您的错误在于声明变量链接:
我想它一定在递归函数之外。
在函数内部,每次都会重新初始化它
var links = [];
$.getJSON("file.json", function getText (oValue, sKey) {
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" );
});
var-links=[];
$.getJSON(“file.json”,函数getText(oValue,sKey){
if(typeof oValue==“object”| | typeof oValue==“array”){
对于(i为椭圆形){
getText(oValue[i],i);
}
}否则{
links.push(““+sKey+” ”;
}
$(“
”{
“类”:“我的新列表”,
html:links.join(“”)
}).附于(“主体”);
});
尝试将函数定义移到事件处理程序之外:
function getText (links, oValue, sKey) {
if (typeof oValue == "object" || typeof oValue == "array") {
for (i in oValue) {
getText (links, oValue [i], i);
}
} else {
if(oValue && sKey)
links.push ( "<li id='" + oValue + "'>" + sKey + "</li>" );
}
$( "<ul/>", {
"class": "my-new-list",
html: links.join( "" )
}).appendTo( "body" );
};
$.getJSON("file.json", function(data, success){
var links = [];
getText (links, data, 0);
});
函数getText(links、oValue、sKey){
if(typeof oValue==“object”| | typeof oValue==“array”){
对于(i为椭圆形){
getText(links,oValue[i],i);
}
}否则{
if(oValue&&sKey)
links.push(“$.each()
发出JSON请求的函数*/
函数dojson(){
/*开始重写OP的示例代码*/
$.getJSON('file.json',函数(json){
var-links=[];
函数getText(key,val){
if(typeof val==“object”| typeof val==“array”){
用于(val中的i){
getText(i,val[i]);
}
}否则{
links.push(“<代码> <代码> > p>因为其他的答案覆盖了大部分你应该考虑的事情,我想我会为你的实际问题发布一个解决方案。p> 您希望遍历任意JSON并搜索特定键,并且可能对其值有一个条件。然后,您希望返回指定键的所有值(如果指定,则通过条件) 假设您拥有以下json:
{
"hello": "some text",
"object": {
"key1": "hello!",
"key2": "Bye!"
},
"array": [{
"some_key1": "blah blah blah",
"some_key2": 24
}, {
"some_key1": "ghiojd",
"some_key2": 13
}],
"numeric_array": [2, 3, 4, 5]
}
此代码段将在上面的json中搜索其值以blah开头的some_key1
:
函数regexpConditionFactory(regex){
返回函数(值){return regex.test(值);};
}
函数searchObjectForKey(对象、键、条件、结果){
如果($.isPlainObject(obj)){
if(对象hasOwnProperty(键)){
if(!condition | |($.isFunction(condition)和&condition(obj[key]))
结果.推送(obj[键]);
}