JSON RKManagedObjectMapping 与 RestKit
我有以下 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 和核心数据对象。
ObjectManager init:
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"
}
]
感谢您的帮助
I have the following JSON structure :
{
"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"
}
]
}
Then i have the following code in my iPhone application to handle the json and core data objects.
ObjectManager init:
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];
Object mapping setup:
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"];
With that configuration it is not working. ObjectLoader does not create or update anything in the coreData datastore.
If i change the JSON structure with the following it works like a charm.
[
{
"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"
}
]
Thank you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上我终于找到了解决方案。只需声明您的对象映射位于 json 响应的特定标记内即可。在我的例子中,通过添加以下行
userMapping.rootKeyPath = @"users"; 来
“用户”希望它对其他人有帮助。
In fact i have finally found the solution. It's only a matter of declaring that your object mapping is inside a specific tag of the json response. In my case "users" by adding the following line
userMapping.rootKeyPath = @"users";
Hope it helps others.
看看:
和
Look at:
and