How to parse a json array that contains multiple datatypes?-Collection of common programming errors
I have this json array:
var a = [{"results":[{"id":"25","name":"John","age":"46"},{"id":"41","name":"Sonny","age":"31"}],"count":2,"total":14}];
It contains an array called “results” and two other variables with numerical values, count and total.
How can i get each of the values of “results”, “count” and “total” from the above array ?
I tried: console.log(a.count);
But it says undefined.
-
First, execute your code will cause
SyntaxError: Unexpected token ILLEGAL
.There is an invalid character after
age
between"age":"31"
.Remove that, then
a
is an array, so you get the first element bya[0]
,then
get the count by
a[0].count
get the results by
a[0].results
-
To load results:
console.log(a[0].results);
To load count:
console.log(a[0].count);
To load total:
console.log(a[0].total);
Originally posted 2013-11-10 00:45:02.