Will Larche
Will Larche
Consultant

Sometimes you spend way too much time googling a question when you have the power to answer it yourself. Man has become increasingly forgetful as it learns to retrieve information from the web. You can read about it here but chances are you won’t retain much. (Don’t believe me? How are your times tables? Name the planets. Go someplace new without your GPS…Yeah I thought so!)

Coding is a career that VERY much suffers from this phenomenon. If something is unknown, we search search search for documentation of someone doing it first. BUT sometimes we have all the tools we need to do something already.

I have some NSManagedObjects. They contain data. I need to send that with JSON over the web.

It seems so simple that I thought ‘There must be a method either in the Apple docs or in my SBJSON framework’. Of course nothing came up on Google so then I thought ‘I’m so stupid; There’s probably a 3rd party framework JUST FOR THIS!’ But, um, duh, no there is not. Because it’s so simple and I already knew how to do it.

To get started, make yourself some categories.

  1. File -> New -> New File

  2. iOS -> CocoaTouch -> Objective-C Category

  3. Category: name it something useful like “JSON_Dicts”

  4. Category on: The class we’re dealing with (the NSManagedObject the CoreData code generator made us). Observe: You now have a category on your class that should be named thusly: “[class name]+JSON_Dicts”.

  5. Declare the following in the new category’s .h file:

- (NSDictionary *)jsonDictionary;

  1. Implement that sucker. For this example I’ll use an NSManagedObject from a project I’m working on involving adding “Animal” entries into a database:

`- (NSDictionary *)jsonDictionary { NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:10]; if (self.age) { [dictionary setObject:self.age forKey:@”age”]; } else { [dictionary setObject:nil forKey:@”age”]; }

if (self.gender) { [dictionary setObject:self.gender forKey:@”gender”]; } else { [dictionary setObject:nil forKey:@”gender”]; }

if (self.namestring) { [dictionary setObject:self.namestring forKey:@”namestring”]; } else { [dictionary setObject:nil forKey:@”namestring”]; }

if (self.numberofanimals) { [dictionary setObject:self.numberofanimals forKey:@”numberofanimals”]; } else { [dictionary setObject:nil forKey:@”numberofanimals”]; }

if (self.size) { [dictionary setObject:self.size forKey:@”sizeint”]; } else { [dictionary setObject:nil forKey:@”sizeint”]; }

if (self.species) { [dictionary setObject:self.species forKey:@”species”]; } else { [dictionary setObject:nil forKey:@”species”]; }

if (self.status) { [dictionary setObject:self.status forKey:@”status”]; } else { [dictionary setObject:nil forKey:@”status”]; }

return dictionary; }`

Das it!

  1. Send that to your JSON encoder. I use SBJSON so my message looks like this:

NSString *jsonString = [animal.jsonDictionary JSONRepresenation];

It’ll come out looking like this (for example):

jsonRepresentation: {"age":2,"namestring":"RMA","gender":1,"numberofanimals":1, "sizeint":2,"status":0,"species":"Cat"}

Anybody have any suggested improvements?

Read these next…

  • Change of Address
    09.03. 2020 | Jonathan Blessing
    Change of Address

    Given the risk presented by the novel coronavirus (SARS-CoV-2), I’ve decided that DOOR3 will be a remote-only organization for the foreseeable future. We are fortunate to benefit from being a...

  • Senior Business Analyst
    07.17. 2020 | DOOR3
    Senior Business Analyst

    Location: New York, USA The Senior Business Analyst will act as an interface between business user groups and IT to understand, document, and track the business needs and requirements throughout...