【问题标题】:Bad Syntax error for URI value after JSON serializationJSON 序列化后 URI 值的语法错误
【发布时间】:2012-09-23 04:33:32
【问题描述】:

对于使用 PUT 的 REST Web 服务调用,我需要发送如下所示的 JSON:

{"leader":"John Smith","leader_id":"asldfkj234234324asldkfs234","resource_uri":"/api/event/38001"}

相反,当我序列化 JSON 时,它会插入额外的“/”字符,这就是为什么我相信我会收到 400 错误:

{"leader":"John Smith","leader_id":"asldfkj234234324asldkfs234","resource_uri":"\/api\/event\/38001"}

这是我正在使用的代码。有什么想法吗?

NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                self.clubToEdit.leader, @"leader",
                                self.clubToEdit.leaderID, @"leader_id",
                                @"/api/event/38001", @"resource_uri",
                                nil];

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDictionary options:0 error:&error];

if (!jsonData) 
{
    NSAssert(FALSE, @"Unable to serialize JSON from NSDict to NSData"); 
} else {
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    RKParams *params = [RKRequestSerialization serializationWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]MIMEType:RKMIMETypeJSON];
    [client put:@"/api/event/?format=json&username=test&api_key=apikey" params:params delegate:self]; 
}

【问题讨论】:

    标签: ios json rest tastypie


    【解决方案1】:

    首先,您的问题与代码不符。在你的问题中,你声明你需要发送"\api\event\38001",而在你的代码中你写的是“/api/event/38001"。由于你的问题只对第二种情况有意义,我假设它是有效的。

    现在的问题是您的 JSON 序列化程序类转义斜杠(一些特殊字符需要转义),这就是它在这些字符之前添加反斜杠的原因。

    【讨论】:

    • 抱歉,修正了错字。我想我的问题仍然存在,我该如何解决?谢谢
    • @RedRobin 你不需要“修复”它。
    • 好的,谢谢,我猜是别的东西抛出了 400 错误。谢谢
    猜你喜欢
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2015-02-07
    • 2018-07-28
    • 2018-11-26
    • 1970-01-01
    相关资源
    最近更新 更多