作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 JSON 结构:
{
"users":
[
{
"user_id":"000228D2-CB0D-4019-ADF9-66FE163E0BBC",
"firstname":"aFirstname",
"name":"aLastName",
"nickname":"user_nickname",
"email":"user_email",
"uri":"user_website",
"birthday":"1976-12-08 00:00:00",
"password":"49722f17a3838181cc9cf351df9d9054e465a778",
"recovery_key":"97011336-3A4C",
"language":"fr_FR",
"timezone":"Europe\/Paris",
"last_update":"0000-00-00 00:00:00",
"creation_date":"2011-11-24 15:03:50",
"status":"20"
}
,
{
"user_id":"100228D2-CB0D-4019-ADF9-66FE163E0BBC",
"firstname":"aFirstname",
"name":"aLastName",
"nickname":"user_nickname",
"email":"user_email",
"uri":"user_website",
"birthday":"1976-12-08 00:00:00",
"password":"49722f17a3838181cc9cf351df9d9054e465a778",
"recovery_key":"97011336-3A4C",
"language":"fr_FR",
"timezone":"Europe\/Paris",
"last_update":"0000-00-00 00:00:00",
"creation_date":"2011-11-24 15:03:50",
"status":"10"
}
]
}
然后我的 iPhone 应用程序中有以下代码来处理 json 和核心数据对象。
对象管理器初始化:
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://json_source_url.com"];
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
NSString *databaseName = @"myDatastore.sqlite";
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName usingSeedDatabaseName:nil managedObjectModel:nil delegate:self];
对象映射设置:
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForEntityWithName:@"User"];
userMapping.primaryKeyAttribute = @"userID";
[userMapping mapKeyPathsToAttributes:@"user_id", @"userID",
@"firstname", @"firstname",
@"name", @"name",
@"nickname", @"nickname",
@"email", @"email",
@"uri", @"uri",
@"birthday", @"birthday",
@"password", @"password",
@"recovery_key", @"recoveryKey",
@"language", @"language",
@"timezone", @"timezone",
@"last_update", @"lastUpdate",
@"creation_date", @"creationDate",
@"status", @"status",
nil];
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"users"];
使用该配置,它不起作用。 ObjectLoader 不会创建或更新 coreData 数据存储中的任何内容。
如果我使用以下内容更改 JSON 结构,它就像一个魅力。
[
{
"user_id":"000228D2-CB0D-4019-ADF9-66FE163E0BBC",
"firstname":"aFirstname",
"name":"aLastName",
"nickname":"user_nickname",
"email":"user_email",
"uri":"user_website",
"birthday":"1976-12-08 00:00:00",
"password":"49722f17a3838181cc9cf351df9d9054e465a778",
"recovery_key":"97011336-3A4C",
"language":"fr_FR",
"timezone":"Europe\/Paris",
"last_update":"0000-00-00 00:00:00",
"creation_date":"2011-11-24 15:03:50",
"status":"20"
}
,
{
"user_id":"100228D2-CB0D-4019-ADF9-66FE163E0BBC",
"firstname":"aFirstname",
"name":"aLastName",
"nickname":"user_nickname",
"email":"user_email",
"uri":"user_website",
"birthday":"1976-12-08 00:00:00",
"password":"49722f17a3838181cc9cf351df9d9054e465a778",
"recovery_key":"97011336-3A4C",
"language":"fr_FR",
"timezone":"Europe\/Paris",
"last_update":"0000-00-00 00:00:00",
"creation_date":"2011-11-24 15:03:50",
"status":"10"
}
]
感谢您的帮助
最佳答案
事实上我终于找到了解决方案。只需声明您的对象映射位于 json 响应的特定标记内即可。在我的例子中,通过添加以下行来“用户”
userMapping.rootKeyPath = @"users";
希望对其他人有帮助。
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForEntityWithName:@"User"];
userMapping.primaryKeyAttribute = @"userID";
userMapping.rootKeyPath = @"users";
[userMapping mapKeyPathsToAttributes:@"user_id", @"userID",
@"firstname", @"firstname",
@"name", @"name",
@"nickname", @"nickname",
@"email", @"email",
@"uri", @"uri",
@"birthday", @"birthday",
@"password", @"password",
@"recovery_key", @"recoveryKey",
@"language", @"language",
@"timezone", @"timezone",
@"last_update", @"lastUpdate",
@"creation_date", @"creationDate",
@"status", @"status",
nil];
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"users"];
关于iphone - JSON RKManagedObjectMapping 与 RestKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8412454/
我有以下 JSON 结构: { "users": [ { "user_id":"000228D2-CB0D-4019-ADF9-66FE163E0BBC", "
初始化 Restkit 时,我在运行时遇到此断言失败: 2011-11-09 08:00:50.817 myapp[32572:207] application:didFinishLaunchingW
我是一名优秀的程序员,十分优秀!