json中的Json值foreach循环

时间:2022-10-25 08:38:47

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 id and type.

如何创建jQuery foreach循环并仅获取id和type的值。

4 个解决方案

#1


15  

    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);
    });

#2


9  

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);
});

Here is that stupid example running

这是运行的愚蠢示例

EDIT:

编辑:

As enoyhs said this could also be achieved with pure javascript which would be a faster solution. Here is a benchmark of looping arrays in javascript vs jQuery:

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

#3


2  

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);

#4


0  

Try:

尝试:

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

#1


15  

    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);
    });

#2


9  

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);
});

Here is that stupid example running

这是运行的愚蠢示例

EDIT:

编辑:

As enoyhs said this could also be achieved with pure javascript which would be a faster solution. Here is a benchmark of looping arrays in javascript vs jQuery:

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

#3


2  

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);

#4


0  

Try:

尝试:

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