2015-12-20 216 views
8

我想在節點js中創建一個JSON對象沒有任何成功。沒有關於每一個項目的大小任何知識如何創建JSON對象節點js

{ 'Orientation Sensor': 
    [ { sampleTime: '1450632410296', 
     data: '76.36731:3.4651554:0.5665419' }, 
    { sampleTime: '1450632410296', 
     data: '78.15431:0.5247617:-0.20050584' } ], 
    'Screen Orientation Sensor': 
    [ { sampleTime: '1450632410296', 
     data: '255.0:-1.0:0.0' } ], 
    'MPU6500 Gyroscope sensor UnCalibrated': 
    [ { sampleTime: '1450632410296', 
     data: '-0.05006743:-0.013848438:-0.0063915867}, 
    { sampleTime: '1450632410296', 
     data: '-0.051132694:-0.0127831735:-0.003325345'}]} 

但在一個動態的方式: 例如創建類似的對象。 有沒有像Node js那樣的東西。 非常感謝你

+0

你有任何額外的代碼就可以顯示你現在怎麼想? – itamar

+1

沒有「JSON」-Object,你是什麼意思? – CoderPi

回答

19

我相信你正在尋找的是陣列的工作爲對象的值的方法:

var o = {} // empty Object 
var key = 'Orientation Sensor'; 
o[key] = []; // empty Array, which you can push() values into 


var data = { 
    sampleTime: '1450632410296', 
    data: '76.36731:3.4651554:0.5665419' 
}; 
var data2 = { 
    sampleTime: '1450632410296', 
    data: '78.15431:0.5247617:-0.20050584' 
}; 
o[key].push(data); 
o[key].push(data2); 

這是標準的JavaScript和不是具體的NodeJS。爲了它序列化到可以使用本機JSON.stringify JSON字符串:

JSON.stringify(o); 
//> '{"Orientation Sensor":[{"sampleTime":"1450632410296","data":"76.36731:3.4651554:0.5665419"},{"sampleTime":"1450632410296","data":"78.15431:0.5247617:-0.20050584"}]}' 
+0

非常感謝,這正是我正在尋找的。 –

4

JavaScript Object()構造函數使一個對象,你可以分配給成員。

myObj = new Object() 
myObj.key = value; 
myObj[key2] = value2; // Alternative 
+0

我沒有downvote,但目前還不清楚什麼問題確切地是要求 – CoderPi

-1

您的JSON代碼是無效的,看到這一點:

{ 
    "Orientation Sensor": [{ 
     "sampleTime": "1450632410296", 
     "data": "76.36731:3.4651554:0.5665419" 
    }, { 
     "sampleTime": "1450632410296", 
     "data": "78.15431:0.5247617:-0.20050584" 
    }], 
    "Screen Orientation Sensor": [{ 
     "sampleTime": "1450632410296", 
     "data": "255.0:-1.0:0.0" 
    }], 
    "MPU6500 Gyroscope sensor UnCalibrated": [{ 
     "sampleTime": "1450632410296", 
     "data": "-0.05006743:-0.013848438:-0.0063915867" 
    }, { 
     "sampleTime": "1450632410296", 
     "data": "-0.051132694:-0.0127831735:-0.003325345" 
    }] 
} 
0

其他的答案是有幫助的,但JSON在問題無效。我已經格式化它,使其低於清晰,注意缺少的單引號在線路24

1 { 
    2  'Orientation Sensor': 
    3  [ 
    4   { 
    5    sampleTime: '1450632410296', 
    6    data: '76.36731:3.4651554:0.5665419' 
    7   }, 
    8   { 
    9    sampleTime: '1450632410296', 
10    data: '78.15431:0.5247617:-0.20050584' 
11   } 
12  ], 
13  'Screen Orientation Sensor': 
14  [ 
15   { 
16    sampleTime: '1450632410296', 
17    data: '255.0:-1.0:0.0' 
18   } 
19  ], 
20  'MPU6500 Gyroscope sensor UnCalibrated': 
21  [ 
22   { 
23    sampleTime: '1450632410296', 
24    data: '-0.05006743:-0.013848438:-0.0063915867 
25   }, 
26   { 
27    sampleTime: '1450632410296', 
28    data: '-0.051132694:-0.0127831735:-0.003325345' 
29   } 
30  ] 
31 } 

有關於如何在JavaScript中操縱對象了很多偉大的文章(是否使用節點JS或瀏覽器)。我建議這裏是一個良好的開端: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

+1

謝謝,我對Java Script不太瞭解。我會幫助:] –