简体   繁体   English

为什么JSON.parse不能用于“23232323”?

[英]Why is JSON.parse not working for “23232323.”?

I have this field 我有这个领域

text = "23232323."

and for validation I want to get it like this 23232323. 为了验证我想得到它像23232323.

I am trying with JSON.parse(text) giving SyntaxError: JSON.parse: missing digits after decimal point 我正在尝试使用JSON.parse(text)给出SyntaxError: JSON.parse: missing digits after decimal point

and eval(text) is giving 23232323 eval(text)给出23232323

How can I fix it? 我该如何解决?

It can't be parsed because, contrary to popular belief, JSON is a strongly defined language and you can't simply put anything that you could eval. 它无法解析,因为与流行的看法相反,JSON是一种强烈定义的语言,你不能简单地放置任何你可以评估的东西。

Here's how numbers are defined : 以下是数字的定义方式:

在此输入图像描述

(see json.org ) (见json.org

You can solve the problem by removing the dot at the end or, better, by not putting it in the first place. 您可以通过删除最后的点来解决问题,或者更好的是,不要将它放在首位。 Of course, if you trust the source, you can simply use eval('('+yourstring+')') too. 当然,如果您信任源,您也可以简单地使用eval('('+yourstring+')')

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

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