2016-03-29 10 views
1

フィールド(テキスト)sqliteにJsonオブジェクトを保存してから、NSDictionaryまたはNSMutableArrayを選択して再変換してキー/値を解析します。jsonオブジェクトをsqliteのテキストのように保存し、読み込み時に辞書に再変換

これは見ての通り、私はsqliteのDBに実際に

enter image description here

を保存する方法である、iTunesのAPIからの曲のオブジェクトです。私はそのオブジェクトを読んでそれを解析したい。

これは私が充填しながら、私は選択クエリを作成し、NSDictionaryの中でそれを保存する方法であると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; 
} 

私はcellForRowAtIndexPathメソッド

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

でそれを読み、しようとしてみてくださいキーアートワークの値を取得する

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

エラーが発生する

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

私はいくつかの方法で辞書/ nsmutablearraysを台無しにしていることを理解しています。助けが必要です。ありがとう!

EDIT:これは私がDBにデータを保存する方法です

NSString *query = [NSString stringWithFormat:@"insert into canciones (idplaylist, cancion) values ('%@', '%@')", [[globals.arrayMyPlaylists objectAtIndex:indexPath.row] valueForKey:@"id"], self.elementoSeleccionado]; 
[self.dbManager executeQuery:query]; 

self.elementoSeleccionadoは「cancion」オブジェクトにNSMutableArrayのであり、それは最初の画像を示しているようにそれが保存されます。

EDIT 2:これは私が

enter image description here

EDIT 3 schmidt9の答えをしようとして得るものです:OK、私は、JSON文字列をエスケープています。どのように私は今解析する必要がありますか?

enter image description here

+0

'cancion'全体を単に文字列として保存して辞書に解析せずに、まずJSONオブジェクトに構文解析する必要があります。 – schmidt9

+0

look、私はあなたの言うことに応じて質問を編集しました – Quetool

+0

行ごとに1つのjsonオブジェクトを保存する – schmidt9

答えて

0

あなたがNSJSONSerializationで最初にあなたの出力を解析する必要があります

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

// I suppose your array canciones should contain only one song object ... 
NSError *error = nil; 
id object = [NSJSONSerialization JSONObjectWithData:canciones[0] options:0 error:&error]; 

if(error) { 
    // handle error ... 
} 

if([object isKindOfClass:[NSDictionary class]]) 
{ 
    NSDictionary *result = object; 
    [dictionary setObject:result forKey:@"canciones"]; 
} 

編集

2オプションのデータベースへの保存:
- あなたが準備したJSON文字列を取得する場合、 DBに直接保存しますが、引用符をエスケープする前に保存してください。たとえばを参照してください。 here
- あなたはNSDictionary持っている場合:その後

- (NSString*)JSONStringWithDictionary:(NSDictionary*)dictionary prettyPrinted:(BOOL)prettyPrinted 
{ 
    NSJSONWritingOptions options = (prettyPrinted) ? NSJSONWritingPrettyPrinted : 0; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:options error:nil]; 
    return [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; 
} 

をあなたも文字列をエスケープする必要があります。

+0

見た目を編集する!私はあなたに答えます! – Quetool

+0

NSDataとしてデータベースに保存する必要があります。 – Quetool

+0

hm、あなたのスクリーンショットに関しては、2つの配列が1つずつラップされ、基本的に不変の配列 - >可変配列 - > jsonオブジェクト...オブジェクトを取得するために 'canciones [0] [0]'私のスニペットの出力をチェックして、私はそれをテストしていません – schmidt9

関連する問題