[英]Javascript printing to html with percent sign
I get a bunch of JSON data through an XHR request, and get the following data back: 我通过XHR请求获得了一堆JSON数据,并返回了以下数据:
{"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: 然后,我尝试遍历这些数据,并使用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.
即使我删除了JSON响应中的百分号,我仍然得到了错误的数字。
Assume that the variable is valid, I'm using a library that maps the json variable to json.data_list. 假设变量是有效的,我正在使用一个库,该库将json变量映射到json.data_list。
I AM NOT SO BRIGHT 我不太聪明
Somewhere the XHR request got mangled by Dojo. XHR请求在某个地方被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]
DEMO: http://jsfiddle.net/7bgvV/ 演示: http : //jsfiddle.net/7bgvV/
var len = json.data-list.length;
looks like a subtraction operation to the JavaScript VM. 看起来像JavaScript VM的减法运算。 You need to use this method:
json['data-list'].length.
您需要使用以下方法:
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\"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.