jQuery foreach 循环中的 Json 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5743866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:44:52  来源:igfitidea点击:

Json values in jQuery foreach loop

jqueryjson

提问by Simpanoz

I'm getting the following JSON response from the server:

我从服务器收到以下 JSON 响应:

[{"id":"1","pid":"0","type":"Individual","code":"i","status":"1"},
 {"id":"2","pid":"0","type":"Group","code":"g","status":"1"},
 {"id":"15","pid":"0","type":"asdasd","code":"asd","status":"1"},
 {"id":"16","pid":"0","type":"asdas","code":"asd","status":"1"},
 {"id":"17","pid":"0","type":"my check","code":"mt","status":"1"}]

How can I make jQuery foreach loop and get only values of idand type.

如何制作 jQuery foreach 循环并仅获取id和 的值type

回答by Sotomajor

    var json = '[{"id":"1","pid":"0","type":"Individual","code":"i","status":"1"},{"id":"2","pid":"0","type":"Group","code":"g","status":"1"},{"id":"15","pid":"0","type":"asdasd","code":"asd","status":"1"},{"id":"16","pid":"0","type":"asdas","code":"asd","status":"1"},{"id":"17","pid":"0","type":"my check","code":"mt","status":"1"}]';
    $.each($.parseJSON(json), function() {
        alert(this.id + " " + this.type);
    });

回答by Dalen

var json = 
[
   {"id":"1","pid":"0","type":"Individual","code":"i","status":"1"},
   {"id":"2","pid":"0","type":"Group","code":"g","status":"1"},
   {"id":"15","pid":"0","type":"asdasd","code":"asd","status":"1"},
   {"id":"16","pid":"0","type":"asdas","code":"asd","status":"1"},
   {"id":"17","pid":"0","type":"my check","code":"mt","status":"1"}
];

$.each(json,function(i,el)
{
   alert(el.id+' - '+el.type);
});

Hereis that stupid example running

是运行的愚蠢示例

EDIT:

编辑:

As enoyhssaid this could also be achieved with pure javascript which would be a faster solution. Hereis a benchmark of looping arrays in javascript vs jQuery:

正如enoyhs所说,这也可以通过纯javascript来实现,这将是一个更快的解决方案。 是 javascript 与 jQuery 中循环数组的基准测试:

回答by ezmilhouse

Working sample here: http://jsfiddle.net/ezmilhouse/emCT8/

这里的工作示例:http: //jsfiddle.net/ezmilhouse/emCT8/

Snippet to create new array of objects that only contain id and type keys:

创建仅包含 id 和 type 键的新对象数组的片段:

var json = [{"id":"1","pid":"0","type":"Individual","code":"i","status":"1"},{"id":"2","pid":"0","type":"Group","code":"g","status":"1"},{"id":"15","pid":"0","type":"asdasd","code":"asd","status":"1"},{"id":"16","pid":"0","type":"asdas","code":"asd","status":"1"},{"id":"17","pid":"0","type":"my check","code":"mt","status":"1"}];

var arr = [];
$.each(json, function(key, value){
    arr.push({
        id: value.id,
        type: value.type
    });
});

console.log(arr);

回答by uzername_not_found

Try:

尝试:

<script type="text/javascript">
$(document).ready(function () {
    var x = { "A" : {"A1": "1" } };
        $.each(x, function(i,v) {
            alert(i);
            console.log(i);
        });
});
</script>