我正在研究嵌入式系統,我的整個過程理論遵循以下方法: 1.向後端發送命令,要求以JSON格式存儲帳戶信息。 2.後端寫入一個包含所有賬戶和相關信息的JSON文件(可能有0到16個賬戶)。 3.下面是我陷入困境的地方 - 使用JavaScript(我們使用JQuery庫)遍歷特定元素(與XPath類似)的返回信息,並根據發現的元素數構建一個數組,下拉框選擇您想要查看的帳戶,然後使用帳戶信息進行操作。 所以我的代碼如下所示:通過特定元素的JSON數據迭代 - 類似於XPath
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
}
所以我需要的是JSON版本的XPath打造voip.accounts.label的陣列。 第一個帳戶信息看起來是這樣的:
{
"result_set": {
"voip.accounts[0]": {
"label": "Dispatch1",
"enabled": true,
"user": "1234",
"name": "Jane Doe",
"type": "sip",
"sip": {
"lots and lots of stuff":
},
}
}
}
難道我對這個問題複雜化?任何人都可以拋棄的智慧將不勝感激。
如果您的JSON實際上包含一個數組,您可以將該數組作爲變量並遍歷它。 –
我在@ Confusion的答案是正確的,但我決定作弊。我最終在後端創建了一個流程函數,它只是爲我提供了一組我正在尋找的標籤。然後,當做出選擇時,我只是將索引彈出到'/info?q=voip.accounts []'中,並且我有我的關聯帳戶。過度思考很爛....感謝@Kevin B和@Confusion的幫助。 – Highroller