这是我的代码,我想要计算其中的对象数量。
使用eval函数,我能够获得元素,但不知道如何计算其中的对象总数。有人能帮帮我吗??
var txt = '{
"employees": [{
"a": "wkn",
"d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services",
"n": "",
"u": "http://wipro.com/",
"t": ["outsourcing", "offshore", "india"],
"dt": "2009-06-26T10:26:02Z"
}, {
"a": "shaktimaan",
"d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services",
"n": "Wipro Technologies is the No 1 provider of integrated business, technology and process solutions on a global delivery platform.",
"u": "http://wipro.com/",
"t": ["Indian", "IT", "Services", "Companies"],
"dt": "2011-09-16T17:31:25Z"
}, {
"a": "tonemcd",
"d": "Offshore Outsourcing | IT Services",
"n": "",
"u": "http://wipro.com/",
"t": ["outsourcing", "IT"],
"dt": "2007-11-04T03:53:18Z"
}]
}'; //added \n for readability
var obj = eval ("(" + txt + ")");
document.getElementById("fname").innerHTML=obj.employees[1].a
document.getElementById("lname").innerHTML=obj.employees[1].u 这是我得到的回应:
First Name: shaktimaan
Last Name: http://wipro.com/我能够获取elemtns,但我需要对象计数。
发布于 2012-01-05 13:27:22
您可以使用length
obj.employees.length这是JSFiddle:http://jsfiddle.net/wavXY/3/
我推荐使用JSON.parse而不是eval。请通过此链接了解更多详细信息:http://www.json.org/js.html
发布于 2012-01-05 13:45:50
作为一个JS新手,看着你的员工数组,我自己想知道它是如何被遍历的。我做了一些实验,也许这不是最有效的方法,但它帮助我理解了如何遍历和计算这样的东西。
这就是问题所在:http://jsfiddle.net/bSMQn/
var txt = '{"employees":[{"a": "wkn", "d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services", "n": "", "u": "http://wipro.com/", "t": ["outsourcing", "offshore", "india"], "dt": "2009-06-26T10:26:02Z"}, {"a": "shaktimaan", "d": "Wipro Technologies \u2013 IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services", "n": "Wipro Technologies is the No 1 provider of integrated business, technology and process solutions on a global delivery platform.", "u": "http://wipro.com/", "t": ["Indian", "IT", "Services", "Companies"], "dt": "2011-09-16T17:31:25Z"}, {"a": "tonemcd", "d": "Offshore Outsourcing | IT Services", "n": "", "u": "http://wipro.com/", "t": ["outsourcing", "IT"], "dt": "2007-11-04T03:53:18Z"}]}';
var obj = eval ("(" + txt + ")");
var i =0;
for (;i<obj.employees.length;i++)
{
document.writeln("Employee " + i+":<br/><br/>");
var j = 0;
for(v in obj.employees[i])
{
j++;
document.writeln(v + " => " + obj.employees[i][v] +"<br/>");
}
document.writeln("<b>Count:" + j +"</b>");
document.writeln("<hr/><br/>");
}输出
Employee 0:
a => wkn
d => Wipro Technologies – IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services
n =>
u => http://wipro.com/
t => outsourcing,offshore,india
dt => 2009-06-26T10:26:02Z
Count:6
Employee 1:
a => shaktimaan
d => Wipro Technologies – IT Services, Product Engineering Solutions, Technology Infrastructure Services, Business Process Outsourcing, Consulting Services
n => Wipro Technologies is the No 1 provider of integrated business, technology and process solutions on a global delivery platform.
u => http://wipro.com/
t => Indian,IT,Services,Companies
dt => 2011-09-16T17:31:25Z
Count:6....etc
希望能有所帮助。
https://stackoverflow.com/questions/8738070
复制相似问题