我對JavaScript相當陌生。從我看起來像這樣的SQL Server數據庫retreive數據:在json中返回表格數據的格式
[Object { shortcode="0013A2004031AC9A", latest_measurement=1067, keyid="6801"},
Object { shortcode="0013A2004031AC9A", latest_measurement=7, keyid="6802"},
Object { shortcode="0013A2004031AC9A", latest_measurement=8598838, keyid="6803"}]
我想在這樣的JSON格式如下:
{mac : 0013A2004031AC9A, keys : {6801:1067, 6802:7, 6803:8598838}}
,但我就是不明白這一點。
我有超過以上和每一個新的MAC,我覺得我做的JSON對象
var jsonDataPerMac = {};
我循環:
jsonDataPerMac[i]={"mac": device.shortcode, "keys":[]};
,但我怎麼填補了鑰匙? 任何提示將不勝感激。 enter code here
var macs = [];
var jsonDataPerMac = {};
var i = 0;
$.ajax({
url: "/bmmeasurements",
type: "GET",
data: {"unitid" : unitid},
async: false,
success: function (data) {
console.log(data);
initializeTable();
$.each(data, function (index,device) {
//add all distinct macs in an array, to use them as a column header
if($.inArray(device.shortcode, macs) == -1) {
macs.push(device.shortcode);
jsonDataPerMac[i]={"mac": device.shortcode, "keys":[]};
i++;
//create a table cell for each possible key. id = 'mac-key'
createTableGrid(device.shortcode);
}
//add the measurement data to the correct cell in the grid
$('#' + device.shortcode + '-' + device.keyid).html(device.latest_measurement);
});
}});
謝謝,這完全符合我的要求。我非常感謝你的幫助。 – Cindy
歡迎您:)您可以隨時將我的答案標記爲有用,這對我來說會是一點回報。 –
畢竟它不完全是我想要做的,但在我的例子中可能不清楚:{mac:0013A2004031AC9A,keys:{「6801」:1067,「6802」:7,「6803」:8598838}} 6801,6082和6803必須是諸如密鑰和mac之類的標識符。我想解決newArray.keys.6801能夠做不同的鍵值之間的計算和比較。 – Cindy