Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery从JSON返回所有项_Javascript_Jquery_Json - Fatal编程技术网

Javascript jQuery从JSON返回所有项

Javascript jQuery从JSON返回所有项,javascript,jquery,json,Javascript,Jquery,Json,JavaScript: function loadDoc(url) { $.ajax({ url: 'mytestjson', dataType: 'json', cache: false }).success(function (result) { console.log(result); //var txt = result.newBranches[0].newNonBranchLocation;

JavaScript:

function loadDoc(url) {
    $.ajax({
        url: 'mytestjson',
        dataType: 'json',
        cache: false
    }).success(function (result) {
        console.log(result);
        //var txt = result.newBranches[0].newNonBranchLocation;
        $.each(result.newBranches, function(index, txt) 
        {
                // change this to FALSE when ready
                if (newNonBranchLocation.indexOf('TRUE') > -1) {
                        $('#btbumpin').hide();
                    }
            else {
                $('#btbumpin').show();
            }
        });
    })
}
JSON:

我有上面的jQuery和JSON。我想返回
newBranches
中的所有值,但我目前只从第一个
newBranches
对象获取值

    "cn": "11111101",
    "newNonBranchLocation": "TRUE",
    "newBranchNumber": "111111"
我想返回
111111 01true11111111111111111102true111111
等等
i、 e.返回一个字符串,该字符串与
newBranches

中的所有值连接。您可以使用双精度
$。Each()
交叉JSON并将值添加到缓冲区

$(函数(){
var json={
“errorNode”:{
“错误代码”:0,
“errorMsg”:“,
“errorSev”:”
},
“新牧场”:[{
“cn”:“11111101”,
“newNonBranchLocation”:“TRUE”,
“newBranchNumber”:“111111”
}, {
“cn”:“11111102”,
“newNonBranchLocation”:“TRUE”,
“newBranchNumber”:“111111”
}, {
“cn”:“11111103”,
“newNonBranchLocation”:“TRUE”,
“newBranchNumber”:“111111”
}, {
“cn”:“11111181”,
“newNonBranchLocation”:“TRUE”,
“newBranchNumber”:“111111”
}]};
$('body').append(dumpContentOfJSON(json.newBranches));
});
函数dumpContentOfJSON(json){
var缓冲区=“”;
$.each(json、函数(索引、数组)
{
buffer+=dumpArray(数组);
});
返回缓冲区;
}
函数转储数组(数组){
var缓冲区=“”;
$.each(数组、函数(索引、值){
缓冲区+=值;
});
返回缓冲区;
}

在循环外声明一个变量,例如

var output = '';
在循环中,您可以将所有需要的值附加到变量中

output + = txt.cn + txt.newNonBranchLocation + txt.newBranchNumber;

循环完成后,您可以使用此变量,该变量将包含附加的字符串。

连接所有cn、newNonBranchLocation和newBranchNumber值,并返回单个字符串?@DVJex yes这就是我的意思want@topcat3那你为什么不呢?@zerohero它只返回前3个结果111111 01true111111。我希望它能循环通过所有的,但我想不出来,谢谢,如果我只是想循环通过并获得所有新的非分支位置,例如。TRUETRUETRUETRUE@topcat3使用变量并单独使用
txt.newNonBranchLocation
。如
output2+=txt.newNonBranchLocation
output + = txt.cn + txt.newNonBranchLocation + txt.newBranchNumber;