-4

How do I print values of my fetched json object with each.

This is what i do

$.each(data, function(key, value){
    console.log(key + ": " + value);
});

And this is what value contains.

Object
->Person: Array[1]
    -->0: Array[10]
         --->0: "id:8"
         --->1: "name:Holly"
    --->length: 1
    --->__proto__: Array[0]
->__proto__: Object

And this is what i get

0: [object Object]
1: [object Object]
2: ........
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
user1729425
  • 131
  • 5
  • 11
  • I can get the id by doing $.each(data, function(key, value){ $.each(value, function(key, value){ $.each(value, function(key, value){ console.log(key + ": " + value[0]); }); }); }); But I dont think this is pretty... – user1729425 Dec 17 '12 at 10:11
  • 1
    Look at [Printing nested JSON without using variable names](http://stackoverflow.com/questions/4110004/printing-nested-json-without-using-variable-names) question. In short, you need a recursion. – andrewpey Dec 17 '12 at 10:12
  • What do you mean by `print`? – Sang Suantak Dec 17 '12 at 10:12
  • 1
    Just take some good browser, no need to do anything else. – zerkms Dec 17 '12 at 10:12

2 Answers2

1

Either do

console.log(data) 

or

console.log(JSON.stringify(data))
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-1

Value is an object, so it prints like the object.

if you want the data in the object,

you will need to print each one separately as follows:

Value[0][0] ec..

Hodaya Shalom
  • 4,327
  • 12
  • 57
  • 111
  • I still have to do console.log(key + ": " + value.Producer[0][1]); When I use .id I get undefined – user1729425 Dec 17 '12 at 10:25
  • The id is in the array? First place? So Value [0][0] will give the id – Hodaya Shalom Dec 17 '12 at 10:28
  • Problem is that i will get strings like name:Holly name:Jonathan So there is not a way to write the loop so that it can look like value.Person[0]['name'] And this will be the utput Holly Jonathan – user1729425 Dec 17 '12 at 10:48
  • You want to print the name or the ID? – Hodaya Shalom Dec 17 '12 at 11:05
  • It does not matter wich one, but lets say i want the id Then i have to do the loop, then I need to fetch the value and split it where : is and then I can do what i want with the value Like var id = value.Producer[0][0].split(':'); console.log(id), will output "8" but is there a way to do this in a more simple way with the loop. – user1729425 Dec 17 '12 at 11:12
  • 1
    I do not think there is a way shorter, sorry. – Hodaya Shalom Dec 17 '12 at 11:15