Question : JSON Value Retrieve

Hi,
I have following JSON (let's call it result). How can I iterate the list of items in the children by searching through id (i.e., "test")?

Via Array, it would be
result[0].children

Instead of direct access to array like result[0],
I want something where I search through result for "test" and iterate over the list of items in children -

Thanks,
P
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
[{
    id : "test"
    children: [{
        url:http://www.google.com,
        text: "Google",
        checked: true
    }, {
        url:http://www.yahoo.com,
        text: "Yahoo",
        checked: true
    }]
}]

Answer : JSON Value Retrieve

>Instead of direct access to array like result[0], I want something where I search through result for "test" and iterate over the list of items in children

var result = getJSON("test").children;
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
var json = [
		{
			id : "tost",
			children: [{
				url:"http://www.google.com",
				text: "Google",
				checked: true
			}, {
				url:"http://www.yahoo.com",
				text: "Yahoo",
				checked: true
			}]
		},
		{
			id : "test",
			children: [{
				url:"http://www.experts-exchange.com",
				text: "EE",
				checked: true
			}, {
				url:"http://stackoverflow.com",
				text: "Stack",
				checked: true
			}]
		}
	];
	function getJSON(id) {
		for(var i in json) if(json[i].id == id) break;
		return json[i];
	}
	function init() {
		result = getJSON("test").children;
		for(var i=0;i<result.length;i++) {
			alert( "children URL : " + result[i].url );
			alert( "children text : " + result[i].text );
			alert( "children checked : " + result[i].checked );
		}
	}
</script>
</head>
<body onload="init();">
</body>
</html>
Random Solutions  
 
programming4us programming4us