im new to json. Ive created objects similar in structure to the attached json string. I believe what ive created, intentionally, is a nested array object...
{
"tracelog":{
"title":"",
"phase":"12",
"users":"",
"logduration":"",
"logdurationtype":"undefined",
"showlogduedate":"no",
"durationstartfield":"",
"fields":[
{
"atts":[
{
"name":"dfvdfv",
"fieldtype":"text",
"followedbydate":"no",
"datetype":"---",
"fieldduration":"---",
"fielddurationtype":"undefined",
"fielddurationstart":"",
"autocalcunits":"none",
"validationtype":"none",
"fieldvisibleto":"",
"fieldaccessibleto":"",
"dropdown":""
}
]
},
{
"atts":[
{
"name":"ffff",
"fieldtype":"text",
"followedbydate":"no",
"datetype":"---",
"fieldduration":"---",
"fielddurationtype":"undefined",
"fielddurationstart":"",
"autocalcunits":"none",
"validationtype":"none",
"fieldvisibleto":"",
"fieldaccessibleto":"",
"dropdown":""
}
]
}
]
}
}
What i want to do is find the length of the array named "atts". Ive parsed the JSON string as follows...
var obj = jQuery.parseJSON(jsonstring);
... and later call the objects thusly ...
obj.tracelog.fields.length
accurately tells me how many fields there are.
obj.tracelog.fields[0].atts.name
... gives me the name, but throws a console error saying field[0] is undefined. so it works, and it doesnt. while ....
obj.tracelog.fields[0].atts.length
does NOT give me the number of fields in the "atts" array (if it is infact an array - id like it to be) but gives me "undefined".
Can someone point out where im going wrong here?
thanks.
obj.tracelog.fields[0].attsis actually an array. So this could not have worked:obj.tracelog.fields[0].atts.name. It wouldn't have errored out, but it should have returnedundefined, not the name. You would have needed to use:obj.tracelog.fields[0].atts[0].nameObject.keys(obj.tracelog.fields[0].atts[0]).lengthin ES5 compliant hosts.