Javascript 如何创建 JSON 对象 Node js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34385499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create JSON object Node js
提问by daniel the man
i am trying to create an JSON object in Node js without any success. for example to create an object like:
我正在尝试在 Node js 中创建一个 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'}]}
but in a dynamic way without any knowledge about the size of every item. is there something like that for Node js. thank you very much
但以动态方式,无需了解每个项目的大小。Node js有没有类似的东西。非常感谢您
回答by paolobueno
What I believe you're looking for is a way to work with arrays as object values:
我相信您正在寻找的是一种将数组用作对象值的方法:
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);
This is standard JavaScript and not something NodeJS specific. In order to serialize it to a JSON string you can use the native JSON.stringify
:
这是标准的 JavaScript 而不是 NodeJS 特定的东西。为了将其序列化为 JSON 字符串,您可以使用本机JSON.stringify
:
JSON.stringify(o);
//> '{"Orientation Sensor":[{"sampleTime":"1450632410296","data":"76.36731:3.4651554:0.5665419"},{"sampleTime":"1450632410296","data":"78.15431:0.5247617:-0.20050584"}]}'
回答by Dylan Kirkby
The JavaScript Object()
constructor makes an Object that you can assign members to.
JavaScriptObject()
构造函数创建一个对象,您可以为其分配成员。
myObj = new Object()
myObj.key = value;
myObj[key2] = value2; // Alternative
回答by Timmeh
The other answers are helpful, but the JSON in your question isn't valid. I have formatted it to make it clearer below, note the missing single quote on line 24.
其他答案很有帮助,但您问题中的 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 }
There are a lot of great articles on how to manipulate objects in Javascript (whether using Node JS or a browser). I suggest here is a good place to start: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
有很多关于如何在 Javascript 中操作对象的好文章(无论是使用 Node JS 还是浏览器)。我建议这里是一个很好的起点:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects