Friday, 14 March 2014

Resolved Empty number fields and NSJSONSerialization


i'm using apple's relatively new foundation class nsjsonserialization convert json data coming rotten tomatoes web service. i'm having trouble empty number fields (e.g., year field empty).

setup: xcode 4.2, arc, rotten tomatoes api restful web service

here's conversion using aforementioned class:
code:
   nserror *error;   nsdictionary *json = [nsjsonserialization jsonobjectwithdata:self.receiveddata options:0 error:&error];  
if log json dictionary object, can see empty year field (note: small portion of logged nsdictionary object):
code:
   title = "heart of lion (coeur de lion)";      year = "";  
but how empty represented? nsnull object? however, following conditional not evaluate yes empty year field (note: amovie dictionary object parsed out of json dictionary object; contains information single movie):
code:
  if ([amovie objectforkey:@"year"] == [nsnull null])        nslog(@"year null!");  
if execute following code exception thrown because year field empty:
code:
  nsnumber *year = [amovie objectforkey:@"year"];  result.year = [year stringvalue]; // throws exception  
so have been doing catching exception (i.e., @try , @catch), there has got better way handle empty number field.

thanks.
 

i haven't used apple's json class yet should able figure out class year. i'll guess it's string not number. may it's number if valid , string if invalid, might pain.

anyway, class of year object? should visible in variables pane in debugger. otherwise, text appears in debugger console exception? these should tell class.
 


Forums iPhone, iPad, and iPod Touch iOS Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment