我想借助魔法熊猫的 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 数据库的内容时,我在成分表中有两个相等的条目:
数据库内容的图像:http: //twitpic.com/8fa0k3
目前我有点无助,要解决这个问题,如果有人可以帮助我,我会很高兴。