RestKit 0.20 JSON对象被序列化为POST主体中的GET样式请求

时间:2013-01-17 10:33:16

标签: ios json restkit

我刚开始使用RestKit 0.20.0而且我在创建格式良好的JSON请求时遇到了问题。

我得到了这个(来自其他工具包的日志):

request.body=title=A%20glorious%20walk%20in%20the%20woods&startDateTime=2013-01-13%2016%3A09%3A33%20%2B0000&endDateTime=2013-01-13%2016%3A09%3A43%20%2B0000&points[][longitude]=-122.0307725&points[][latitude]=37.3310798&points[][longitude]=-122.0307334&points[][latitude]=37.33154242&points[][longitude]=-122.03075743&points[][latitude]=37.33138305&points[][longitude]=-122.03075659&points[][latitude]=37.33131185&points[][longitude]=-122.03057969&points[][latitude]=37.33156519&points[][longitude]=-122.03075535&points[][latitude]=37.33144466&points[][longitude]=-122.03076342&points[][latitude]=37.33123666&points[][longitude]=-122.03074488&points[][latitude]=37.33149482&points[][longitude]=-122.03068145&points[][latitude]=37.33155419&points[][longitude]=-122.03062909&points[][latitude]=37.33156564&points[][longitude]=-122.03076853&points[][latitude]=37.33115792

当我需要这个(带有大括号的普通json对象和points属性的数组)时:

{
    title: "Something",
    startDateTime: "dateinfo",
    endDateTime: "moredateinfo",
    points: [
        {
            latitude: "37.33131313",
            longitude: "122.4325454"
        },
        {
            latitude: "37.33131313",
            longitude: "122.4325454"
        }
    ]
}

我有两个主要对象:DLWalk包含一个DLPoint对象的NSSet(它们是CoreData对象,但目前我忽略了它,只关注创建HTTP请求)

以下是我用来创建请求的代码:

// Point mapping
RKObjectMapping *mappingPoint = [RKObjectMapping requestMapping];
[mappingPoint addAttributeMappingsFromArray:@[@"latitude", @"longitude"]];
RKRequestDescriptor *reqDescPoint = [RKRequestDescriptor requestDescriptorWithMapping:mappingPoint objectClass:[DLPoint class] rootKeyPath:nil];

// Walk mapping
RKObjectMapping *mappingWalk = [RKObjectMapping requestMapping];
[mappingWalk addAttributeMappingsFromArray:@[@"endDateTime", @"startDateTime", @"title"]];
RKRequestDescriptor *reqDescWalk = [RKRequestDescriptor requestDescriptorWithMapping:mappingWalk objectClass:[DLWalk class] rootKeyPath:nil];


// Define the relationship mapping
[mappingWalk addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"points" toKeyPath:@"points" withMapping:mappingPoint]];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://192.168.1.10:8080"]];
                            [manager addRequestDescriptor:reqDescWalk];
                            [manager addRequestDescriptor:reqDescPoint];
                            [manager addResponseDescriptor:responseDescriptor];

// POST to create
[manager postObject:walk path:@"/walk/save" parameters:nil success:nil failure:nil];

所以问题是:为什么我的POST主体中没有看到正常的JSON对象?

1 个答案:

答案 0 :(得分:14)

你得到的是request.body是URL编码的,这是RESTKit的默认行为,通常可以正常工作。

如果您希望它是JSON编码的,只需在发布查询之前插入此行

manager.requestSerializationMIMEType=RKMIMETypeJSON;

有关这方面的更多信息,请查看RKObjectManager类的API文档: requestSerializationMIMEType