6

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

[{"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.

4 Answers 4

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);
    });
Sign up to request clarification or add additional context in comments.

2 Comments

why would you convert the object to a string and then parseJSON instead assign the object to json variable. any specific reason?
No, I thought the string has been given initially. Of course JSON if it's an object itself, shouldn't be parsed.
10
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:

1 Comment

Or you can get without jQuery with simple: for (i in json) { alert(json[i].id + ' - ' + json[i].type); }
2

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

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

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

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.