parsing - Count JSON objects Javascript -
ok theres json object:
jsontext = {"switches":[{"dpid":"00-00-00-00-00-01"},{"dpid":"00-00-00-00-00-02"},{"dpid":"00-00-00-00-00-03"},{"dpid":"00-00-00-00-00-04"},{"dpid":"00-00-00-00-00-05"},{"dpid":"00-00-00-00-00-06"},{"dpid":"00-00-00-00-00-07"}], "links":[["00-00-00-00-00-01","00-00-00-00-00-05"],["00-00-00-00-00-02","00-00-00-00-00-03"],["00-00-00-00-00-05","00-00-00-00-00-06"],["00-00-00-00-00-05","00-00-00-00-00-07"],["00-00-00-00-00-02","00-00-00-00-00-04"],["00-00-00-00-00-01","00-00-00-00-00-02"]]}
the goal count dpid objects exist in it... next far:
function objectlength(obj) { var result = 0; for(var prop in obj) { if (obj.hasownproperty(prop)) { // or object.prototype.hasownproperty.call(obj, prop) result++; } } homecoming result; }
the function phone call amount of dpid's in switches
objectlength(jsontext.switches);
the issue is, when there 1 dpid switches this:
jsontext = {"switches":[{"dpid":"00-00-00-00-00-01"}],"links":[]}
it counts 0... doesn't in section... reason assume empty guess..?
tl;dr why when 1 dpid of switches exists json object returns 0..
cheers!
since switches
property only seems contain objects dpid
properties, don't have special count them. this:
var count = jsontext.switches.length;
javascript parsing object
No comments:
Post a Comment