2

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

6
  • It is an array...use for-loop... instead of for-in.. make sure you are ot iterating JSON...You must have Object/Array Commented Mar 31, 2016 at 10:04
  • @RayonDabre you mean like that? for(var i = 0; i < data.length; i++) { var obj = data[i]; console.log(obj.id); }. That didn't work :( Commented Mar 31, 2016 at 10:06
  • Yes, I doubt it is object/array...Check typeof data Commented Mar 31, 2016 at 10:06
  • @RayonDabre typof data says its a string :O Commented Mar 31, 2016 at 10:08
  • 2
    Then you must parse it before looping..JSON.parse(data); Commented Mar 31, 2016 at 10:09

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It is JSON not Array..Question says The Response is every letter not every object
Yes you are right about it, but it can be looped using the for loop.
from the given input (json) of the OP you will get objects when you loop, you can test my snippet and you will see it working.
Go through the comments and you will get to know..You have to make out that it is JSON from The Response is every letter not every object
Yes indeed you are right as it is a string in the OP, for my example I used the given input which is an array as you can see.
1

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

Comments

0

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.

Comments

0

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. 

Comments

0

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)

});

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.