2013-07-31 151 views
0

我從數據庫中的JSON數據來動態地,見下圖:如何從JSON中提取創建一個新的JSON文件

[["15","0.027","0.137","0.353","0.044","0.111","0.024","2013-07-30 17:45:06"],["17","0.027","0.137","0.353","0.044","0.111","0.024","2013-07-30 17:50:14"],["19","0.017","0.137","0.353","0.044","0.111","0.024","2013-07-30 17:55:35"],["21","0.017","0.137","0.353","0.044","0.111","0.024","2013-07-30 18:00:34"],["23","0.017","0.137","0.353","0.044","0.111","0.024","2013-07-30 18:05:10"],["25","0.017","0.137","0.353","0.044","0.111","0.024","2013-07-30 18:10:07"] and so on.. 

所以結構如下[time1, time2, time3, time4, time5, time5, time7, TimeStamp]

我需要實現什麼在結束了7個新JSONs具有結構如下:

json1 = [time1,TimeStamp] 
json2 = [time2,TimeStamp] 
json3 = [time3,TimeStamp] 
json4 = [time4,TimeStamp] 
and so on.. 

我需要這個落實到jQPLot 任何人都可以幫助嗎?謝謝

+1

術語說明,您所擁有的都不是JSON。這只是JS數組。 –

+0

它是來自php文件的json_encode($ data)。你能幫忙嗎?非常感謝 – medzi

+0

Rory的意思是JSON格式不同,它由{}和更多的規則所包圍。你有什麼是一個JS數組。如果你想得到正確的答案,最好是正確地使用術語。即使他們可能知道答案,很多人也不會明白你的意思。解決方案可能是Scorchio在這裏的答案。 – Chilpol

回答

0

檢查這個(你可以找到一個ready-to-play jsfiddle here):

// Creates a new array from the items array with the item 
// for the given index and with the last item (timestamp). 
function timeNFromItems(items, index) { 
    return [items[index], items[items.length - 1]]; 
} 

// Loop over each time index, so that from: 
// [time1, time2, time3, time4, time5, time6, time7, timestamp] 
// ...the following is printed out: 
// time1, timestamp 
// time2, timestamp 
// [...] 
// time7, timestamp 
function timesFromItems(items) { 
    numberOfTimes = 7; 
    result = ""; 
    for (var i = 0; i < numberOfTimes; ++i) { 
     result += timeNFromItems(items, i) + "\n"; 
    } 
    return result; 
} 

sample = [ 
    ["15", "0.027", "0.137", "0.353", "0.044", "0.111", "0.024", "2013-07-30 17:45:06"], 
    ["17", "0.027", "0.137", "0.353", "0.044", "0.111", "0.024", "2013-07-30 17:50:14"], 
    ["19", "0.017", "0.137", "0.353", "0.044", "0.111", "0.024", "2013-07-30 17:55:35"], 
    ["21", "0.017", "0.137", "0.353", "0.044", "0.111", "0.024", "2013-07-30 18:00:34"], 
    ["23", "0.017", "0.137", "0.353", "0.044", "0.111", "0.024", "2013-07-30 18:05:10"], 
    ["25", "0.017", "0.137", "0.353", "0.044", "0.111", "0.024", "2013-07-30 18:10:07"] 
]; 

alert(timesFromItems(sample[0])); 

第一個函數獲取給定對的「行」,第二個剛剛遍歷得到所有對。

+0

非常感謝,那工作:) – medzi