2012-01-05 8 views
0

내 코드는 여기에있는 객체의 수를 계산하고 싶습니다.자바 스크립트에서 객체를 계산하는 방법

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를 가져올 수 있었다,하지만 난 개체 수를 원한다.

+1

eval을 사용하지 마십시오. JSON.parse를 사용하십시오. –

답변

1

직원 번호 배열을 보면서 나 자신이 어떻게 지나갈 수 있는지 궁금해했습니다. 조금 실험했는데 가장 효율적인 방법은 아니지만 트래버스하고이를 계산하는 방법을 이해하는 데 도움이되었습니다. 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 

.... 등

는 희망이 도움이 :

는 여기에 바이올린입니다.

관련 문제