gpt4 book ai didi

iphone - JSON RKManagedObjectMapping 与 RestKit

转载 作者:行者123 更新时间:2023-12-03 21:04:49 25 4
gpt4 key购买 nike

我有以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com