So I have the JSON below, as an example:
[{"x":"12","y":"−67"},{"x":"12","y":"−68"},{"x":"13","y":"−70"}]
After I parse it into an object, using JSON.parse
, I end up with an array of objects. So far so good. But when I try to parse those "x" and "y" coordinates into integers, I just get NaN
no matter what! Tried all kinds of solutions; parseInt(obj.x)
, +obj.x
, Number(obj.x)
, etc..! Just can’t understand why I can’t convert those properties to integers?
2
Answers
Your numeric strings contain invisible Unicode formatting (directional override) characters. Furthermore, the minus signs are true typographical minus signs (U+2212) instead of the typewriter hyphen-minus characters (U+002D), and
parseInt
is unable to parse the former.Ideally, you would fix the JSON so that it contain numbers directly (instead of wrapped in strings). But if that is outside your control, you need to remove the formatting characters and replace the minus sign with the hyphen-minus before using
parseInt
.If you really cannot change the data, then try this: