0

我按照此链接https://github.com/RestKit/RestKit/wiki/Posting-NSDictionary-as-JSON创建 json 帖子,并从服务器获取 json 响应。如何继续处理对对象列表的 json 响应?

- (void)sendAsJSON:(NSDictionary*)dictionary {

    RKClient *client = [RKClient clientWithBaseURL:@"http://restkit.org"];        

    // create a JSON string from your NSDictionary 
    id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
    NSError *error = nil;
    NSString *json = [parser stringFromObject:dictionary error:&error];

    // send your data
    if (!error)
        [[RKClient sharedClient] post:@"/some/path" params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON] delegate:self];

}


- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
{
    NSLog(@"after posting to server, %@", [response bodyAsString]);
}

EDIT1:这是我要发布到服务器的 Json。

{
    "memberId": "1000000",
    "countryCode": "US",
    "contacts": [
        {
            "phoneNumber": "+12233333333",
            "memberId": "2222",
            "contactId": "123456",
            "name": "john"
        },
        {
            "phoneNumber": "+12244444444",
            "memberId": "3333",
            "contactId": "123457",
            "name": "mary"
        }
    ]
}

EDIT2:实际上有人在另一个线程中解决了这个问题。 https://stackoverflow.com/a/7726829/772481

4

2 回答 2

4

使用 RKObjectManger 而不是 RKClient 来执行 POST。然后,您可以在调用此方法时将响应加载到对象中:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects

编辑(给定 JSON 发送到服务器):

您可以创建自定义模型类,而不是按照您当前的方式创建 JSON。

首先,您可以为您的顶级对象创建一个模型类(假设它被称为用户)。

用户标题

//  User.h

#import <Foundation/Foundation.h>

@interface User : NSObject

@property (nonatomic) int memberId;
@property (nonatomic, copy) NSString *countryCode;
@property (nonatomic, strong) NSArray *contacts;

@end

用户实现

//  User.m

#import "User.h"

@implementation User

@synthesize memberId;
@synthesize countryCode;
@synthesize contacts;

@end

然后,您可以创建一个名为 Contact 的模型类。

联系人标题

//  Contact.h

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property (nonatomic, strong) NSString *phoneNumber;
@property (nonatomic) int memberId;
@property (nonatomic) int contactId;
@property (nonatomic, strong) NSString *name;

@end

联系实施

//  Contact.m

#import "Contact.h"

@implementation Contact

@synthesize phoneNumber;
@synthesize memberId;
@synthesize contactId;
@synthesize name;

@end

您可以这样使用这些类:

Contact *john = [[Contact alloc] init];
john.phoneNumber = @"+12233333333";
john.memberId = 2222;
john.contactId = 123456;
john.name = @"john";

Contact *mary = [[Contact alloc] init];
mary.phoneNumber = @"+12244444444";
mary.memberId = 3333;
mary.contactId = 123457;
mary.name = @"mary";

User *user = [[User alloc] init];
user.memberId = 1000000;
user.countryCode = @"US";
user.contacts = [NSArray arrayWithObjects:john, mary, nil];

RKObjectMapping *contactsMapping = [RKObjectMapping mappingForClass:[Contact class]];
[contactsMapping mapKeyPath:@"phoneNumber" toAttribute:@"phoneNumber"];
[contactsMapping mapKeyPath:@"memberId" toAttribute:@"memberId"];
[contactsMapping mapKeyPath:@"contactId" toAttribute:@"contactId"];
[contactsMapping mapKeyPath:@"name" toAttribute:@"name"];

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[User class]];
[objectMapping mapKeyPath:@"memberId" toAttribute:@"memberId"];
[objectMapping mapKeyPath:@"countryCode" toAttribute:@"countryCode"];
[objectMapping mapKeyPath:@"contacts" toRelationship:@"contacts" withMapping:contactsMapping];

//Then you set up a serialization mapping and object mapping and POST it

//This method takes care of both the serialization and object mapping
[[RKObjectManager sharedManager].mappingProvider registerMapping:objectMapping withRootKeyPath:@"user"]; 

//POST it
[[RKObjectManager sharedManager] postObject:user delegate:self];

在向您展示如何进行序列化映射和 POST 之前,我需要知道您期望什么样的 JSON 响应。

编辑(给定从服务器返回的 JSON)

要设置序列化映射和对象映射并发布它,您需要设置资源路径(我在启动应用程序时会这样做):

RKObjectRouter *router = [RKObjectManager sharedManager].router;
[router routeClass:[User class] toResourcePath:@"/users" forMethod:RKRequestMethodPOST];

您的资源路径可能不是“/users”。

看一下注释下的代码//Then you set up a serialization mapping and object mapping and POST it,我在其中添加了序列化映射、对象映射和 POST。

于 2012-07-04T01:30:11.353 回答
0

通常在这种情况下您会使用RKObjectLoader,可以通过RKObjectManager@ill_always_be_a_warriors 所说的方式访问。您使用它的原因是因为它包含自由对象映射(如果已配置)。需要注意的一件事是您直接发布“对象”而不是 JSON。有关更多信息,请尝试查看RestKit 示例以了解如何执行此操作,然后如果您仍然有问题,请在此处发布问题。

于 2012-07-04T06:54:26.947 回答