简体   繁体   English

nodejs json.parse保留科学计数法

[英]nodejs json.parse preserve scientific notation

Can anyone help me figure out a JSON.parse() issue I'm facing? 谁能帮助我解决我面临的JSON.parse()问题? This is utilizing Node.js v6.9. 这是利用Node.js v6.9。

Basically, I need to store some data within my database EXACTLY as I see it in a file. 基本上,正如我在文件中看到的那样,我需要在数据库中完全存储一些数据。 While I have the vast majority of this working, there's a couple kinks I'm trying to iron out. 尽管我完成了大部分工作,但我还是想解决一些问题。

First issue is when I run across scientific notation within an array. 第一个问题是当我遇到数组中的科学计数法时。 When I parse this, it does NOT preserve "en" unless it's greater than 6. Thus, the result is that I get a non-scientific notation value stored in an array. 当我对此进行解析时,除非它大于6,否则它不会保留“ en”。因此,结果是我得到了存储在数组中的非科学计数法值。 Is there some way to force the parse() function to preserve this? 有什么方法可以强制parse()函数保留它吗? See examples I ran from my debugger within Visual Studio Code: 请参阅我在Visual Studio Code中从调试器运行的示例:

JSON.parse('[3.908e-3, -5.775e-7]');
Array[2] [0.003908, -5.775e-7]

JSON.parse('[3.908e-4, -5.775e-7]');
Array[2] [0.0003908, -5.775e-7]

JSON.parse('[3.908e-5, -5.775e-7]');
Array[2] [0.00003908, -5.775e-7]

JSON.parse('[3.908e-6, -5.775e-7]');
Array[2] [0.000003908, -5.775e-7]

JSON.parse('[3.908e-7, -5.775e-7]');
Array[2] [3.908e-7, -5.775e-7]

Second issue is another formatting issue. 第二个问题是另一个格式问题。 JSON.parse doesn't seem to preserve the decimals. JSON.parse似乎不保留小数。 Like the first issue above, is it possible to make JSON.parse() preserve the number notation exactly as its given? 像上面的第一个问题一样,是否可以使JSON.parse()完全按其给定的格式保存数字符号? Notice how the parse is chopping off the zero decimal. 注意解析是如何截取零小数的。

JSON.parse('[1.0, 2.0]')
Array[2] [1, 2]

Was reading up on reviver functions that you can pass as a second argument to the parse() function, but I'm a bit lost on these. 正在阅读可以作为parse()函数的第二个参数传递的reviver函数,但是我对这些有点迷惑。 Do I need to write my own custom function to preserve the format as is? 我需要编写自己的自定义函数来按原样保留格式吗?

Edit: More information provided: I'm trying to take a complex JSON object as-is, and store the entire structure in my Postgresql database. 编辑:提供的更多信息:我试图按原样使用复杂的JSON对象,并将整个结构存储在我的Postgresql数据库中。 The file itself is initially uploaded as a Uint8Array. 文件本身最初是作为Uint8Array上传的。 I then do a toString() on that, and do a JSON.parse on the result. 然后,我对此执行toString(),并对结果执行JSON.parse。 So if this was the original contents of the uploaded file, I can do this: 因此,如果这是上载文件的原始内容,我可以这样做:

uploadedFile.toString();
"{
    "someKey": {
      "id": "COG32",
      "control_id": "00D2FFFF0000",
      "coefficients": [3.908e-3, -5.775e-7]
    }
}"

Then I can do this: 然后我可以这样做:

JSON.parse(uploadedFile.toString());
{
    "someKey": {
      "id": "COG32",
      "control_id": "00D2FFFF0000",
      "coefficients": [3.00908, -5.775e-7]
    }
}

Some of the properties in the file I'm given contain arrays of two scientific notation values. 我得到的文件中的某些属性包含两个科学计数值的数组。

In order to do this, I'm parsing the json string to an object and able to iterate the keys and all its properties to store everything within its own record in the database. 为了做到这一点,我将json字符串解析为一个对象,并能够迭代键及其所有属性,以将所有内容存储在数据库中自己的记录中。 This all works great, except for the fact that JSON.parse is automatically transforming 3.908e-3 into 3.003908. 除了JSON.parse自动将3.908e-3转换为3.003908的事实之外,这一切都很好用。
I also need to recreate this json object EXACTLY as it was given to me, from the database. 我还需要从数据库中完全重新创建该json对象。 I understand that they are the "same" numbers. 我知道它们是“相同”的数字。 But when it comes to diffing the files (the original json object and the one I re-create from the database...), they are very different. 但是,在差异化文件时(原始的json对象和我从数据库中重新创建的对象...),它们是非常不同的。 .toExponential() seems to give me the exponential variant of the number, but in string format. .toExponential()似乎给了我数字的指数变体,但是是字符串格式。 If I were to use that, I would wind up with: 如果要使用它,我会得出以下结论:

["3.908e-3", "-5.775e-7"]

When what I want is: 当我想要的是:

[3.908e-3, -5.775e-7]

In your JSON, the data is stored as a type NUMBER; 在您的JSON中,数据以NUMBER类型存储; when you write 3.908e-4 it is equal to 0.0003908. 当您编写3.908e-4时,它等于0.0003908。

You then have 2 choices: 然后,您有2个选择:

  • store your data as a string so it keeps the format (which is ok if you do not want to perform calculations) JSON.parse('["3.908e-3", "-5.775e-7"]'); JSON.parse('[“ 3.908e-3”,“ -5.775e-7”]');将数据存储为字符串,以便保留格式(如果您不希望执行计算就可以了)。
  • force a format when you print the data eg (0.0004345).toExponential() --> 4.345e-4 or (1).toFixed(1) --> 1.0; 打印数据时强制使用格式,例如(0.0004345).toExponential()-> 4.345e-4或(1).toFixed(1)-> 1.0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM