Get Updates By E-mail 

Article Written By:
Johann Dowa

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.


  •  
  •  
  •  
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments