Iterate Through JSON Data for Specific Element - Similar to XPath
Posted
by
Highroller
on Stack Overflow
See other posts from Stack Overflow
or by Highroller
Published on 2012-05-30T15:20:08Z
Indexed on
2012/05/30
16:40 UTC
Read the original article
Hit count: 186
I am working on an embedded system and my theory of the overall process follows this methodology: 1. Send a command to the back end asking for account information in JSON format. 2. The back end writes a JSON file with all accounts and associated information (there could be 0 to 16 accounts). 3. Here's where I get stuck - use JavaScript (we are using the JQuery library) to iterate through the returned information for a specific element (similar to XPath) and build an array based on the number of elements found to populate a drop-down box to select the account you want to view and then do stuff with the account info. So my code looks like this:
loadAccounts = function()
{
$.getJSON('/info?q=voip.accounts[]', function(result)
{
var sipAcnts = $("#sipacnts");
$(sipAcnts).empty(); // empty the dropdown (if necessarry)
// Get the 'label' element and stick it in an array
// build the array and append it to the sipAcnts dropdown
// use array index to ref the accounts info and do stuff with it
}
So what I need is the JSON version of XPath to build the array of voip.accounts.label. The first account info looks something like this:
{
"result_set": {
"voip.accounts[0]": {
"label": "Dispatch1",
"enabled": true,
"user": "1234",
"name": "Jane Doe",
"type": "sip",
"sip": {
"lots and lots of stuff":
},
}
}
}
Am I over complicating the issue? Any wisdom anyone could thrown down would be greatly appreciated.
© Stack Overflow or respective owner