0

I've figured how to iterate through the following JSON as seen here:

{"dummmysetsJSONArr":[{"entryID":"1","distance":"100","calories":"50"},{"entryID":"2","distance":"200","calories":"100"},{"entryID":"3","distance":"300","calories":"150"},{"entryID":"4","distance":"400","calories":"200"},{"entryID":"5","distance":"500","calories":"250"},{"entryID":"6","distance":"600","calories":"300"}],"success":1}

Here's how I do this:

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            myLogger(jsonarray[key].entryID + " - " + jsonarray[key].distance + " - " + jsonarray[key].calories);
        }
    }

But the JSON is failing the if check in the for loop.

What could be wrong with my JSON data?

My objective is to use the above for loop to add data to google.visualization.DataTable() as follows:

        data = new google.visualization.DataTable();
        data.addColumn('number', 'distance');
        data.addColumn('number', 'calories');

        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {

                //data.addRow(jsonarray[key].distance);
                //data.addRow(jsonarray[key].calories);
            }
        }
Community
  • 1
  • 1
ANM
  • 65
  • 1
  • 4
  • 11
  • It fails, if `jsonarray` doesn't have the property for one of the values of `key`. You probably meant to do `obj.hasOwnProperty(key)` to prevent your `for...in` loop from traversing up the prototype chain. – Alex W Apr 15 '15 at 17:37
  • thanks Alex .. changed from jsonarray to obj .. now i get the following error in my console ... uncaught typeerror cannot read property 'entryID' of undefined – ANM Apr 15 '15 at 17:47
  • It's because you are still using `jsonarray` instead of `obj`: `myLogger(jsonarray[key].entryID + " - " ...` – Alex W Apr 15 '15 at 17:48

3 Answers3

1

If you know which property contains collection you required then (working example - jsbin):

var obj = {"dummmysetsJSONArr":[{"entryID":"1","distance":"100","calories":"50"},{"entryID":"2","distance":"200","calories":"100"},{"entryID":"3","distance":"300","calories":"150"},{"entryID":"4","distance":"400","calories":"200"},{"entryID":"5","distance":"500","calories":"250"},{"entryID":"6","distance":"600","calories":"300"}],"success":1};


  for (var key in obj.dummmysetsJSONArr) {
            if (obj.dummmysetsJSONArr.hasOwnProperty(key)) {
              document.write(obj.dummmysetsJSONArr[key].distance + " " + obj.dummmysetsJSONArr[key].calories);              
              document.write("<br/>");
            }
        }

In other case the best way is to find all objects which have entryId, distance and caloreis property because it can be possible that your json could have more arrays (I don't know if this is dynamic json or what).

Adam Łepkowski
  • 2,048
  • 1
  • 24
  • 41
  • thanks Adam ... The JSON isnt dynamic in terms of the number if arrays in the JSON data I'm retrieving the JSON data from from a DB as seen here But out of further interest how do i go about using the approach you suggest whereby I'll be finding all objects which have entryId, distance and caloreis properties – ANM Apr 16 '15 at 08:04
  • Actually forget my last comment .. and I appreciate what has turned out to be the correct answer – ANM Apr 16 '15 at 09:37
0

Looks like you need multiple iterations over key/values. The first iteration will get you the key 'dummmysetsJSONArr' with and array as the value.

You then want to iterate over the array, each key will be the index and the value will be the object.

The next iteration over the value will give you the attribute name of the object and the value.

Ivo
  • 535
  • 2
  • 13
0

The property "dummmysetsJSONArr" is not an object but an array so you could do it like this:

var myObj = {"dummmysetsJSONArr":[{"entryID":"1","distance":"100","calories":"50"},{"entryID":"2","distance":"200","calories":"100"},{"entryID":"3","distance":"300","calories":"150"},{"entryID":"4","distance":"400","calories":"200"},{"entryID":"5","distance":"500","calories":"250"},{"entryID":"6","distance":"600","calories":"300"}],"success":1}
var obj = myObj.dummmysetsJSONArr; //this is an array not an object
var str = "";
for (var i=0;i< obj.length;i++) {     
    str += obj[i].entryID + " - " + obj[i].distance + " - " + obj[i].calories+"<br>";
}

However, arrays in JavaScript are just objects with numerical keys so you can do it like this:

var obj = myObj.dummmysetsJSONArr; //this is an array not an object
var str = "";
 for (key in obj) {     
            str += obj[key].entryID + " - " + obj[key].distance + " - " + obj[key].calories+"<br>";
    }  

Checking for hasOwnProperty doesn't make much sense since your JSON object is just a plain object and every property is its own property

Steve Mc
  • 3,433
  • 26
  • 35