I want to convert a string number (an integer represented as a string. ex "12" ) to a number
Model Class
export class Person{
FirstName : string | undefined ;
Telephone: number | undefined ;
}
TS file
console.log("Person" + JSON.stringify(this.person));
The JSON appears as
{
"FirstName ": "hhhhh",
"Telephone": "1478525",
}
The Telephone property is a number and I want it to appear as follows (without the ""_ :
{
"FirstName ": "hhhhh",
"Telephone": 1478525,
}
Approaches I took.
this.person.Telephone = parseInt(telephone);
Above didn't work, it shows the same number enclosed as a string. I also tried the following:
this.person.Telephone as number
both approaches didn't work. Can someone help me solve this ?
Error I get if I don't convert to a number:
"The JSON value could not be converted to System.Int32. Path: $.Telephone | LineNumber: 2 | BytePositionInLine: 18."
parseIntis the way to go when you want to convert a string to a number. If you want more help you need to create an minimal reproducible example because"Above didn't work, "is not descriptive enough asthis.person.Telephone = parseInt(telephone);should work just fine.