0

I received data from a JSON web service and converted it to an NSArray. I've saved the NSArray in my database for retrieval at a later date. The data in the database looks like this:

(
        {
        ResponseCount = 2;
        ResponseTotal = 8;
    },
        {
        ResponseCount = 6;
        ResponseTotal = 8;
    }
)

Now I'm at the point where I want to take that data and turn it back into an NSArray. I try doing something along the lines of

NSArray *responseArray = self.CoreDataTable.ResponseArray; //NSString value

or

NSArray *responseArray = (NSArray *) self.CoreDataTable.ResponseArray; //NSString value

But when this runs, the NSArray comes back with no objects in it. Any ideas what is wrong?


Code to clarify data saving process.

CoreDataTable *coreDataTable = [NSEntityDescription insertNewObjectForEntityForName:@"CoreDataTable" inManagedObjectContext:self.managedObjectContext];
coreDataTable.CategoryName = [d objectForKey: @"CategoryName"];
coreDataTable.ResponseArray = [d objectForKey: @"ResponseArray"];
[self.managedObjectContext save:nil];
proudgeekdad
  • 3,424
  • 6
  • 42
  • 40
  • are you using jsonkit or not? – Nimit Parekh Feb 08 '13 at 05:28
  • You say “I've saved the NSArray in my database”. Show us the code that saves it. – rob mayoff Feb 08 '13 at 05:30
  • Good question. The JSON from my web service is consumed using the iOS native NSJSONSerialization. – proudgeekdad Feb 08 '13 at 05:32
  • Updated the question to show the save for Rob – proudgeekdad Feb 08 '13 at 05:44
  • json kits hadels itself all the things, one thing which confusing me is that your data is not in array form, replace "(" with "[" and ")" with "]", it might work, check this hack... – AsifHabib Feb 08 '13 at 05:53
  • Try using NSMutableArray. – esh Feb 08 '13 at 05:54
  • 2
    Why not just store the raw JSON and then when you read it convert to NSArray using NSJSONSerialization? – Joel Feb 08 '13 at 06:01
  • I found that in Core Data, you can store an array as a "Transformable" data attribute rather than a string. When you get the data back from the database, it is an NSArray and you can just loop through it. Check here for more info: http://stackoverflow.com/questions/1562676/best-practice-array-dictionary-as-a-core-data-entity-attribute – proudgeekdad Feb 10 '13 at 03:06

0 Answers0