Within a JSON String
, I have IDs as keys, represented as Strings
of numbers, while the values are actual Numbers
(Float
). The problem arises when parsing this information to obtain an object in Safari 10.1.2...
var jsonData = "{\"53352\":0.6, \"82008\":0.4}";
var parsedData = JSON.parse(jsonData);
console.log(parsedData);
Upon parsing, the resulting object looks like:
{
"0": NaN,
"1": NaN,
"2": NaN,
"3": NaN,
"4": NaN,
"5": NaN,
"6": NaN,
"7": NaN,
...
However, the expected outcome should be:
(as observed in Firefox 55.0.3)
Object {53352=0.6, 82008=0.4}
(as seen in Safari 11.0)
{
"53352": 0.6,
"82008": 0.4
}
The issue with Safari stems from its misinterpretation of the String
keys as Number
, leading to an array structure with indices up to 82008. This is unintended. How can I ensure Safari interprets the keys correctly as Strings
?
Possible Solutions Attempted
One approach involves adding an underscore prefix to all keys on the server side, like _53352
instead of 53352
. While this resolves the issue, it necessitates removing the underscore on the client side. However, this method feels unconventional and inconsistent since underscores are not added to IDs elsewhere. Are there other ways to guarantee proper interpretation as String
s?
Consider using org.json for encoding JSON data on the server side.