dk1990
dk1990

Reputation: 309

Json response in javascript is not looping correctly

I have a JsonResponse which looks like this:

    [{"pk": 1, "fields": {"email": "[email protected]", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "[email protected]", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}]

I have tried to access the data like this:

    for (var key in data) {
            console.log(key)
            console.log(data[key]);
        }

The Response is every letter not every object. Which is a bit weird, after looking through the answers on stackoverflow, i tried the other ways as well but always get the same result.

The Result should be the group_name and the group_id. Can anyone help me with that? Thank you in advance

Upvotes: 2

Views: 106

Answers (5)

cнŝdk
cнŝdk

Reputation: 32145

In fact this is an array, so you can simply loop it using a classic for loop, this is an exmaple snippet:

    var data = [{"pk": 1, "fields": {"email": "[email protected]", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
            {"pk": 2, "fields": {"email": "[email protected]", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

            for (var i=0; i<data.length; i++) {
               console.log(data[i]); //You will get an object
               console.log(data[i].pk);
            }

In each iteration you will get an object and then you can access its properties.

EDIT:

It dependes on the type of data here, if it's a string like you mentioned in comments, you should parse it using JSON.parse(data); first then you can loop throught it.

Otherwise if it is an array you will just need to directly loop throught its elements.

Upvotes: 2

Kunal Gadhia
Kunal Gadhia

Reputation: 360

Try using $.each instead of for loop :

Try this :

var object = [{"pk": 1, "fields": {"email": "[email protected]","locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "[email protected]", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

$.each(object, function(index, singleObject) {
   console.log("Single Object :%O",singleObject);
});

In console you will get object properly.

For Fiddle Link Click Here : Fiddle Link

Upvotes: 1

Bouraoui KACEM
Bouraoui KACEM

Reputation: 821

you can try the bellow simple instruction :

var array=[{"pk": 1, "fields": {"email": "[email protected]", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "[email protected]", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

array.forEach(function(obj){
 //to get the current object 
  console.log(obj)
 // to access to the attribute of the current object 
// example 
console.log(obj.fields.email)

});

Upvotes: 0

viki
viki

Reputation: 13

response is in encoded manner,for access you want to decode response.

var x=[{"pk": 1, "fields": {"email": "[email protected]", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},{"pk": 2, "fields": {"email": "[email protected]", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}]; 

var object=JSON.parse(x);//you will get an object then you can find what you want. 

Upvotes: 0

viveksinghggits
viveksinghggits

Reputation: 680

Here you go..

<script>
var object =  [{"pk": 1, "fields": {"email": "[email protected]", "locations": [1], "group_id": "spott", "group_name": "spott"}, "model": "grouping"},
{"pk": 2, "fields": {"email": "[email protected]", "locations": [1, 2], "group_id": "spottalle", "group_name": "spott alle"}, "model": "grouping"}];

for(var i=0; i<object.length; i++){
    console.log(object[i].pk);
    console.log(object[i].fields.email);
    console.log(object[i].fields.group_id);
}


</script>

Please let me know if it is not expected output.

Upvotes: 0

Related Questions