简体   繁体   English

Json 字符串数字格式

[英]Json string number format

I try to understand JSON strings better.我尝试更好地理解 JSON 字符串。 Especially numbers in that format.尤其是那种格式的数字。

Checking if a JSON string is a valid number is easy (as described later), but what are the conventions for parsing a number to a JSON string?检查 JSON 字符串是否为有效数字很容易(如下所述),但是将数字解析为 JSON 字符串的约定是什么?

The number 5 can be easily parsed to "5" , but should I parse 5000 to "5000" , to "5e3" or to "5E3" ?数字5可以很容易地解析为"5" ,但我要分析5000"5000" ,以"5e3""5E3"

Is there a widely accepted (conventions?) minimum/maximum bound for numbers which are too small/too big such that we use e ?对于太小/太大以至于我们使用e数字,是否有一个被广泛接受的(约定?)最小/最大界限?

And a second question on this topic: how would I check if the JSON string stores an integer value.关于这个主题的第二个问题:我将如何检查 JSON 字符串是否存储一个整数值。 This question arises because "5e-2" is 0.05 .出现这个问题是因为"5e-2"0.05 The string doesn't contain "."该字符串不包含"." but is still a fraction, which is non trivial.但仍然是一小部分,这不是微不足道的。 Of course I could do it with simply parsing the value with Number() (JavaScript) and checking if it is an integer with Number.isInteger() (JavaScript).当然,我可以通过简单地使用Number() (JavaScript) 解析该值并使用Number.isInteger() (JavaScript) 检查它是否为整数来Number.isInteger() But that doesn't help me to understand it really better.但这并不能帮助我真正更好地理解它。 Could I check if the string contains "e-" / "E-" , then check if the following number is bigger than the number of digits before the "."我可以检查字符串是否包含"e-" / "E-" ,然后检查以下数字是否大于"."之前的位数"." / "e" / "E" ? / "e" / "E" ?

To check if a JSON string is a valid number (not integer), I use this regex in JavaScript:要检查 JSON 字符串是否为有效数字(不是整数),我在 JavaScript 中使用了以下正则表达式:

// JavaScript
const isJsonNumberRegex = /^-?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+-]?\d+)?$/m;

function isJsonNumber(n: string): boolean {
    return n.match(isJsonNumberRegex) !== null;
}

The string can start with "-" , then comes a single 0 or a digit 1-9 , then optionally followed by any amount of digits 0-9 .字符串可以以"-"开头,然后是单个0数字1-9 ,然后可选地后跟任意数量的数字0-9 If it is a fraction, it is followed by a "."如果是分数,则后跟"." and then by any amount of digits 0-9 .然后是任意数量的数字0-9 To use exponents it starts by the character "e" or "E" , is optionally followed by "+" or "-" , and then again any amount of digits 0-9 .要使用指数,它以字符"e""E"开头,后跟可选的"+""-" ,然后是任意数量的数字0-9

For example:例如:

 function isJsonNumber(n){ return n.match(/^-?(?:0|[1-9]\\d*)(?:\\.\\d*)?(?:[eE][+-]?\\d+)?$/m) !== null; } console.log(isJsonNumber("-12.34E+56")); // true console.log(isJsonNumber("0.0e-010")); // true console.log(isJsonNumber("+0")); // false console.log(isJsonNumber(".5")); // false console.log(isJsonNumber("a")); // false

The specification for JSON can be found here: https://datatracker.ietf.org/doc/html/rfc4627#section-2.4可以在此处找到 JSON 规范: https : //datatracker.ietf.org/doc/html/rfc4627#section-2.4

Copy of what is said in the above link:上面链接中所说的内容的副本:

The representation of numbers is similar to that used in most programming languages.数字的表示类似于大多数编程语言中使用的表示。 A number contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part.一个数字包含一个整数部分,它可以以一个可选的减号为前缀,后面可以跟一个小数部分和/或指数部分。

Therefore, your test should work per these specifications.因此,您的测试应该按照这些规范进行。

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

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