I get a bunch of JSON data through an XHR request, and get the following data back:
{"data_list" : [[1, "Title1", "0.54%"], [2, "Title2", "2.98%"]]}
Then I try to loop through that data, and put it in the console with console.log:
var len = json.data_list.length;
for(var i = 0; i < len; i++)
{
console.log(json.data_list[i]);
}
But I don't get the same percentages:
[1, "Title1", "0.24%"]
[2, "Title2", "0.00%"]
Anyone know why I would get the right data for the first two fields, but not the same percentages? Even if I take out the percent signs in the JSON response, I still get the wrong numbers.
Assume that the variable is valid, I'm using a library that maps the json variable to json.data_list.
I AM NOT SO BRIGHT
Somewhere the XHR request got mangled by Dojo. Thanks for the help!
"data-list" is an invalid identifier as you expect it to act, so it's interpreted as subtraction. Try using bracket notation:
json["data-list"].length
and
json["data-list"][i]
var len = json.data-list.length;
looks like a subtraction operation to the JavaScript VM. You need to use this method: json['data-list'].length.
If you have no control over the method returning this data, try something like this:
var ajaxStr = '{"data-list" : [[1, "Title1", "0.54%"], [2, "Title2", "2.98%"]]}]';
ajaxStr = ajaxStr.replace(/\"([\w]+)-([\w]+)\"/g, "\"$1$2\"");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.