Reputation: 6635
I am trying to parse a JSON string comprised of numerical values, but I keep getting SyntaxError: Unexpected number.
When I remove the preceding 0's it works, so I can get rid of the 0's easily enough, but I need the values formatted that way for display purposes, is there a simple way for me to parse this into an array of strings instead of an array of numbers?
Also, what would be the simplest way to remove all preceding 0's in the string? I am assuming a regex would be best? But any other suggestions are welcome.
JSON.parse('[26,27,28,29,30,31,01,02,03,04,05,06,07,08,09]');
Upvotes: 1
Views: 2851
Reputation: 18344
It's because of the leading zeros. This works fine:
JSON.parse('[26,27,28,29,30,31,1,2,3,4,5,6,7,8,9]');
JSON definition says integers must not have leading zeros
EDIT To remove the leading zeros:
var str = '[26,27,28,29,30,31,01,02,03,04,05,06,07,08,09]';
str = str.replace(/\b0(\d)/g, "$1");
JSON.parse(str);
Cheers
Upvotes: 4
Reputation: 3584
JSON.parse('["26","27","28","29","30","31","01","02","03","04","05","06","07","08","09"]');
should parse it as an array of strings instead of array of numbers.
Upvotes: 0