So the
This post will focus on parsing the JSON returned from thewebservice.
My personal choice for parsing JSON isthe
Let’s start with a quick recap on what JSON is and how it can beused.
Wikipedia says:
JSON (an acronym forJavaScript Object Notation) is a lightweight text-based openstandard designed for human-readable data interchange. It isderived from the JavaScript programming language for representingsimple data structures and associative arrays,called objects.Despite its relationship to JavaScript,it is language-independent,with parsers available for virtually every programminglanguage.
JSON presents its data as key-value pairs. Each value is referencedby a key name,which is a string. If you were to represent a personin JSON,their name would be referenced by the key “name” like so:“name” : “James”.
So,JSON represents data in a way that can be passed betweenapplications easily. Perfect.
So when parsing data from a webservice,the first thing you shoulddo is figure out your model. Look at an example of the webservice’sresponse and figure out which bits represent objects,arrays ofobjects,fields that belong to an object,etc.
But what kinds of data can JSON represent?
Objects are everythingbetween the braces ( { } ).
Strings are enclosed in quotes.
Numbers aren’t enclosed by anything and just appear as numbers,e.g. 12345.
Arrays are enclosed in square brackets ( [ ] ).
Booleans take the form of the words ‘true’ or ‘false’ (withoutquotes).
Null values are represented as ‘null’ (without quotes).
So an example of JSON using all these data types:
And it’s representation in Objective-C:
#import@interface Person : NSObject { NSString *firstName; *lastName; NSInteger age; NSDictionary *address; NSArray *phoneNumbers; } @end
You may think we’ve missed out some information,such as thedetails of the address,and phone numbers. It’s you decision howyou model your objects. I’ve chosen to store the address details ina dictionary,each value being referenced by a key name,just as itis in JSON. The phone numbers also stored in dictionaries,but thenthose dictionaries put into an array.
If you wanted to,you could create another Class named Address anduse it to store the address details. This may be a moreobject-oriented approach and useful if addresses are used in otherplaces throughout your application without needing to be tied to aPerson.
So now that you have your object model,you need to get the dataout of the JSON and create instances of your model.
SBJSON has a useful SBJsonParser class that can parse an entireJSON object in one line:
SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; NSError *error = nil; *jsonObjects [jsonParser objectWithString:jsonString error:&error]; [jsonParser release],jsonParser nil;
SBJSON treats JSON objects as dictionaries in Objective-C.Depending on the webservice you may get a JSON object as the toplevel object or you may get an array. For this reason,objectWithString:error: has
id jsonObject ];if ([jsonObject isKindOfClass:[NSDictionary class]) // treat as a dictionary,or reassign to a dictionary ivar else NSArray class// treat as an array or reassign to an array ivar.
If the webservice only ever returns one of the two representationsas it’s top level then you can go ahead and assume it will beeither an Array or Dictionary,and not have to worry aboutchecking.
Now you have your JSON data in a format that you can manage viaObjective-C. All you need to do now is iterate over the contents ofthe dictionary/array and create instances of Person to representthem.
One thing that’s important to remember is that literal numbers suchas the age value in our Person example will be wrapped in NSNumberobjects,so we’ll need to call ‘intValue’ on them to get thenumber.
NSMutableArrary *people NSMutableArray arrayfor (*dict in jsonObjects) { Person *newPerson [Person alloc] autorelease]; [newPerson setFirstName[dict objectForKey:@"firstName"[newPerson setLastName"lastName"[newPerson setAge"age"] intValue[newPerson setAddress"address"[newPerson setPhoneNumbers"phoneNumber"];[people addObject:newPerson And there we have it. One final thing to take note of. You’ll notice that I used literalstrings as key names while creating the Person objects. It might bebetter for you to determine each key name that you’ll be using andcreate a string constant for them. That way,when you’re parsingyour data,if you misspell firstName,the compiler can throw anerror (because it won’t match the name of the constant you created)and save you a lot of time debugging when,for some damn reason,you’re not getting any value for @”firtName”!
附:
From JSON String to Objects:
SBJsonParser *parser = [[SBJsonParser alloc] init]; // gives array as output id objectArray = [parser objectWithString:@"[1,2,3]"]; // gives dictionary as output id objectDictionary = [parser objectWithString:@"{\"name\":\"xyz\",\"email\":\"xyz@email.com\"}"];From Objects to JSON String:
SBJsonWriter *writer = [[SBJsonWriter alloc] init]; id *objectArray = [NSArray arrayWithObjects:@"Hello",@"World",nil]; // Pass an Array or Dictionary object. id *jsonString = [writer stringWithObject:objectArray];注:SBjson要求开启arc,方法:targets-->build phased->Compile Sources ->双击输入-fobjc-arc