mardi 29 mars 2016

Save json object in sqlite like text and retransform to dictionary on read

I want to save a Json Object in a field (text) sqlite and then read it again with a select and retransform to NSDictionary or NSMutableArray to parse the key/values

This is how i save actually in the sqlite DB

enter image description here

As you see, is a song object from the iTunes api. I want to read that object and parse it.

This is how i make the select query and save it in a NSDictionary while i filling and NSMutableArary

globals.arrayMyPlaylists = [[NSMutableArray alloc] init];
// Form the query.
NSString *query = @"select * from myplaylists";

// Get the results.
NSArray *listas = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];

for (int i = 0; i < listas.count; i++) {

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    NSLog(@"lista %d %@ %@", i, listas[i][0], listas[i][1]);
    [dictionary setObject:listas[i][0] forKey:@"id"];
    [dictionary setObject:listas[i][1] forKey:@"nombre"];


    NSString *query2 = [NSString stringWithFormat:@"select cancion from canciones where idplaylist = %@", listas[i][0]];
    NSArray *canciones = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query2]];
    [dictionary setObject:canciones forKey:@"canciones"];



    [globals.arrayMyPlaylists addObject:dictionary];
    dictionary = nil;
}

When i try to read it in the cellForRowAtIndexPath method

NSArray *canciones = [[NSArray alloc] initWithArray:[[globals.arrayMyPlaylists objectAtIndex:indexPath.row] valueForKey:@"canciones"]];

and try to get the value for the key artworkUrl100

[cell.tapa1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[canciones objectAtIndex:i] valueForKey:@"artworkUrl100"]]] placeholderImage:nil];

i get the error

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7ff575810600> valueForUndefinedKey:]: this class is not key value coding-compliant for the key artworkUrl100.'

i understand i'm messing up in some way with dictionarys/nsmutablearrays. I need some help. Thanks!

Aucun commentaire:

Enregistrer un commentaire