I am using this array( as an example)
http://pastebin.com/M0ZJgAVs
i am using json_encode() on it and fetching it using javascript
does anyone care to tell me
to get the num rows would i simply use
var numrows = json['numrows'];
and to loop over each row
would i use
for(row in json['rows']) {
}
and then inside that to get to each datapoint would i just use
for(data in row) {
}
or am i completely off track?
If you receive the deserialized JSON data in the JavaScript as an object
json, then the code could looks like followingIn general you receive typically JSON data in deserialized form as an object (if you use
jQuery.ajaxfor example). So the access of the data in JavaScript is very simple. It is just accessing of the fields of the object or accessing of the elements of the array. For performance purpose only it is better to use always an indexed loop as a"for in"loop (see http://www.jslint.com/lint.html#forin). If you access a property of an object (like the propertyrows) it is always better to use dotted notation (json.rows) as an index notation (json[rows]). And the last recommendation is caching property value in a local variable (likevar row=rows[iRow]or varcRows = rows.length) always if you access the property more as one time.You can find interesting information from http://jquery-howto.blogspot.com/2009/06/javascript-for-loop-vs-jquery-each.html about the more effective way to enumerate data in the array (read also the last version of the suggested loop in the comment).
It is also a good idea to verify your JavaScript code in JSLint (see http://www.jslint.com/).