フィールド(テキスト)sqliteにJsonオブジェクトを保存してから、NSDictionaryまたはNSMutableArrayを選択して再変換してキー/値を解析します。jsonオブジェクトをsqliteのテキストのように保存し、読み込み時に辞書に再変換
これは見ての通り、私はsqliteのDBに実際に
を保存する方法である、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:これは私が
EDIT 3 schmidt9の答えをしようとして得るものです:OK、私は、JSON文字列をエスケープています。どのように私は今解析する必要がありますか?
'cancion'全体を単に文字列として保存して辞書に解析せずに、まずJSONオブジェクトに構文解析する必要があります。 – schmidt9
look、私はあなたの言うことに応じて質問を編集しました – Quetool
行ごとに1つのjsonオブジェクトを保存する – schmidt9