Here’s a nice simple to use category from Matthew York called NSObject-ObjectMap that makes it easy to set up custom objects from JSON or XML, and serializing those objects back to JSON or XML.
What’s nice about NSObject-ObjectMap is that it’s dedicated just for this purpose.
This example from the readme show’s how one could take a JSON snippet and convert it into an object:
[cc lang=”objc”]
// JSON snippet
{
Name : “Billy”,
FavoriteColors : [“Red”,”Blue”,”Tangerine”],
FavoritePeople : [{
Name : “Jenny”,
FavoriteColors: [@”Orange”,”Black”],
FavoritePeople: []
},{
Name : “Ben”,
FavoriteColors: [“Silver”,”Emerald”,”Aquamarine”],
FavoritePeople: []
}]
}
// Turn that JSON into an NSDictionary, then into your Person object
// – jsonData is the NSData equivalent of the JSON snippet above.
NSData *jsonData;
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
// Now to create the Person object
Person *newPerson = [NSObject objectOfClass:@”Person” fromJSON:jsonDict];
{/cc]
You can find NSObject-ObjectMap on Github here.
A nice easy to use category for serializing/deserializing objects to and from JSON and XML.
- Open Source iOS Library Providing Easy NSObject To JSON/XML Serialization and Conversion To Parse PFObject
- Open Source iOS Library Providing Automatic NSCoding Support For Your Objects
- Best Resources In iOS Development – August 6th, 2012
- Best Resources In iOS Development – February 20th, 2012
- Open Source: Library For Automatic Serialization/Deserialization To And From JSON Dictionaries
Original article: Nice Simple NSObject Category Allowing Easy Serialization/Deserialization From XML or JSON
©2013 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.