Reputation: 799
I've got a http response, it contains response.data, there's a lot long-integers. When I try to create a blob resource with the data in a following way:
var blob = new Blob([ response.data ], {
type : response.headers('Content-Type')
});
The data is parsed and the numbers are treated as integers, the last 4 digits are getting cut (replaced by 0s) because of how JavaScript processes numbers. When I add a letter to the number e.g. "823758273883758237857823758x" it is treated as a string and processed properly.
How can I force JS to treat the numbers as strings and not as integers without adding any needless letters to them?
EDIT: Example response.data content:
"sep="x","Time","Id","Name","1234512345123451234""
Upvotes: 0
Views: 310
Reputation: 358
In JSON, string is always come with double quote around the content. Like "123" will be parsed as String and 123 will be parsed as number. So I think the root problem is in your data source. Try to convert number as string when you generate the JSON string.
Upvotes: 0
Reputation: 198324
// number
a = '823758273883758237857823758';
console.log(a, JSON.parse(a));
// string
b = '"823758273883758237857823758"';
console.log(b, JSON.parse(b));
// error
c = '823758273883758237857823758x';
console.log(c, JSON.parse(c));
A number is not a string. A number value (not in quotes) will be parsed as the JavaScript number. A string value (in quotes) will be parsed as a string. A string does not need a random character to keep it as a string; but a number with a random character will not make it into a string, but will trigger a parse error.
If this is unsatisfactory, please show more details, including the contents of response.data
, and/or the code that generates it.
Upvotes: 1
Reputation: 3297
you can try String(theNumber)
I don't know where the number is coming from, but you can also force it to be string first, before actually sending it.
I've tried:
JSON.stringify(123)
and JSON.stringify(String(123))
and its result:
123
"123"
, respectively.
Upvotes: 0