JSON RKManagedObjectMapping 与 RestKit

发布于 2024-12-20 05:24:26 字数 3422 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-12-27 05:24:26

事实上我终于找到了解决方案。只需声明您的对象映射位于 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"];

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.


    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"];
策马西风 2024-12-27 05:24:26

看看:

RKObjectMapping* usersMapping = [RKObjectMapping mappingForClass:[MyClassForUserObjects class]];

[[RKObjectManager sharedManager].mappingProvider setMapping:statusMapping forKeyPath:@"users"];

Look at:

RKObjectMapping* usersMapping = [RKObjectMapping mappingForClass:[MyClassForUserObjects class]];

and

[[RKObjectManager sharedManager].mappingProvider setMapping:statusMapping forKeyPath:@"users"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文