2012-01-05 5 views
0

ここに私のコードですが、私はこれの中のオブジェクトの数を数えたいと思います。javascriptでオブジェクトを数える方法

私は要素を取得することができましたが、この中の総オブジェクトを数える方法はわかりません。誰も私を助けてくださいできますか?

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 

....など

はそれが役に立てば幸い:

はここでフィドルです。

関連する問題