2012-01-05 3 views
0

Voici mon code, je veux compter le nombre d'objets dans ceci. En utilisant la fonction eval, j'ai pu obtenir les éléments, mais je ne sais pas comment compter le nombre total d'objets dans cette fonction. Quelqu'un peut-il m'aider s'il vous plaît ??comment compter l'objet en 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 

C'est la réponse que je suis arrivé à cela: le nombre

First Name: shaktimaan 
Last Name: http://wipro.com/ 

j'ai pu aller chercher les elemtns, mais je veux objet.

+1

N'utilisez pas eval, utilisez JSON.parse –

Répondre

1

Être un noob JS, en regardant votre tableau d'employés, je me demandais comment il pourrait être traversé. J'ai expérimenté un peu et peut-être ce n'est pas le moyen le plus efficace, mais cela m'a aidé à comprendre comment traverser et compter quelque chose comme ça.

Voici le violon: 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/>"); 

} 

Sortie

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

Hope it helps.

Questions connexes