Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  82] [ 1]  / answers: 1 / hits: 38449  / 12 Years ago, sat, may 12, 2012, 12:00:00

I have some objects that I'm parsing from json using native browser implementations. Some of the objects' properties are numbers. For the moment, the numbers are parse from json as strings and I use parseInt to cast the string to the int I need.



The problem is that I've got 23 objects I do this with and overall about 80 properties that I'm parsing to ints like this:



if (TheObject && TheObject.TheProperty) {
TheObject.TheProperty = parseInt(TheObject.TheProperty, 10);
}


There are many lines of code that look very similar. Is there a way using prototypes or something to change the way the JSON.parse function works so that each time the parser runs it checks to see if a string property is actually an int and if so cast it directly as such?



Thanks.


More From » javascript

 Answers
45

JSON can handle numbers as follow:



{
TheObject:{
TheProperty:5
}
}


If your property was doublequoted then it's a string else it's a numeric, boolean (true and false values), null or just something that cause parse error.



See http://json.org/


[#85627] Friday, May 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cameron

Total Points: 591
Total Questions: 112
Total Answers: 88

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
cameron questions
;