Nice Simple NSObject Category Allowing Easy Serialization/Deserialization From XML or JSON

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.

FacebookTwitterDiggStumbleUponGoogle Plus

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.

Leave a Reply

Your email address will not be published. Required fields are marked *