Loop Through JSON List How and What is Problem with script?
Following is the Script i am working that will indicate the Feature Detection for Firefox i created a Json list i want to call each to check in the script to see wehther its undefined or not but its not working can someone help me i am too much exhausted....!!
<html>
<head>
<script type="text/javascript">
var JsonList =
{
"CheckSet": [
{
"property": "azimuth",
"result": 0
},
{
"property": "cue",
"result": 0
}
]
};
function Check()
{
//alert('working');
var looptrav;
var looplist = JsonList;
var indicator = 0;开发者_StackOverflow
for(looptrav = 0; looptrav < looplist.CheckSet.length; looptrav++)
{
if(typeof(looplist.CheckSet[looptrav].property) != "undefined")
{
looplist.CheckSet[looptrav].result = 1;
indicator = 1;
document.write ("Result: "+looplist.CheckSet[looptrav].result);
}
}
Check();
</script>
</head>
</html>
JsonList
(or looplist
) is an Object
, not an Array
, so it doesn't have a length
property. It however does have a property CheckSet
which you correctly access further on (with one exception) and which is an Array
.
So you need to change
for(looptrav = 0; looptrav < looplist.length; looptrav++)
to
for(looptrav = 0; looptrav < looplist.CheckSet.length; looptrav++)
and change
looplist[looptrav].result = 1;
to
looplist.CheckSet[looptrav].result = 1;
EDIT: You are missing a closing }
and it's probably not a good idea to document.write
inside <head>
.
Working example: http://jsfiddle.net/qjuVk/
精彩评论