I want to parse a JSON string to a javascript object with the exact values of the JSON string.
When I call JSON.parse
on a JSON string like eg { "someArray": [1.0, 2, 3.7] }
I want exactly the numbers 1.0
, 2
and 3.7
in my object. Unfortunately I get 1
, 2
and 3.7
.
How do I parse a JSON string like the one above in an object with the exact values of the string?
In javascript there is no difference between 1.0 and 1 as all numbers are doubles. As far as your parsed data is concerned there is no difference. Try evaluating [1.0, 2.0, 3.0]
in your browser console.
console.log([1.0, 2.0, 3.0])
If you really need the string representation of nodes in your JSON, you'd have to use a parser, and walk the parse tree. Something like the acorn parser, for example.
Here you are doing a mistake
{ "someArray": [1.0, 2, 3.7] }
Data in your array [1.0,2,3.7]
are integer now and in any programming there are no difference between 1
or 1.0
as integer so change them into string by applying this
{ "someArray": ["1.0", "2", "3.7"] }
Stay trying new 😛
将someArray的值转换为字符串
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.