マジカル パンダの MagicalRecord を使用して、json ファイルを SQLite-CoreData-Database にインポートしたいと思います。これは私のjsonファイルの内容です:
{
"title":"Gebratener Schweinebauch auf dänische Art",
"subtitle":"Stegt flaesk med persillesauce",
"preparation_time":"35 Min.",
"preparation_subtitle":"bei 225°C, nicht vorheizen.",
"components":[
{
"name":"Hauptgericht",
"ingredients": [
{
"name":"1 kg geschälte und gekochte Kartoffeln"
}
]
}
]
}
ファイルの内容を次のコア データ モデルにインポートしようとしています: データ モデルの画像: http://twitpic.com/8fa0y9
次のコードを使用して、json ファイルをインポートします。
- (void)initializeRecipeDatabase {
NSString *resource = [[NSBundle mainBundle] pathForResource:kRecipeInitialDatabaseContentFilename
ofType:kRecipeInitialDatabaseContentFileType];
NSError *parsingError = nil;
NSDictionary *result = nil;
if (NSClassFromString(@"NSJSONSerialization")) {
NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:resource];
[inputStream open];
result = [NSJSONSerialization JSONObjectWithStream:inputStream options:0 error:&parsingError];
}
else {
NSData *jsonData = [NSData dataWithContentsOfFile:resource];
result = [jsonData objectFromJSONData];
}
[Recipe MR_truncateAll];
[[NSManagedObjectContext MR_defaultContext] MR_save];
[Recipe MR_importFromDictionary:result];
[[NSManagedObjectContext MR_defaultContext] MR_save];
}
インポートはエラーなしで機能しますが、SQLite データベースの内容を調べると、Ingredient-Table に 2 つの等しいエントリがあります。
データベースコンテンツの画像: http://twitpic.com/8fa0k3
現在、私はこの問題を解決するために無力であり、誰かが私を助けてくれればとても幸せです.