ios - Retrieving JSON value from NSDictionary -


i have issue nsdictionary populated using json. json data looks , able amount

balance =  {     amount = "33069.42";     currencycode = usd; };  nsstring *amtstr = [[account objectforkey:@"balance"] valueforkey:@"amount"]); nsstring *formattedamt = [nsstring stringwithformat:@"$%.2f", [amtstr floatvalue]]; 

but, in cases, there no value amount , instead amount has nsdictionary in sample below

balance =     {     amount =         {         nil = 1;     };     currencycode =         {         nil = true;     }; }; 

and when try stringwithformat

nsstring *formattedamt = [nsstring stringwithformat:@"$%.2f", [amtstr floatvalue]];  

is throwing

-[__nscfdictionary floatvalue]: unrecognized selector sent instance 0x8a551e0  

any ideas on how amount value in second case? if amount nil, need set amtstr 0.

thanks in advance help.

just check if amtstr string or not, before parsing float:

id amtstr = [[account objectforkey:@"balance"] valueforkey:@"amount"]); if([amtstr iskindofclass:[nsstring class]] ){    string *formattedamt = [nsstring stringwithformat:@"$%.2f", [amtstr floatvalue]]; } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -