【发布时间】:2011-02-04 11:21:46
【问题描述】:
我正在使用 JSON 库从 Objective C 创建一个 JSON POST 请求,如下所示:
NSMutableURLRequest *请求; request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/", host, action]]]; [请求 setHTTPMethod:@"POST"]; [请求 setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"]; NSMutableDictionary *requestDictionary = [[NSMutableDictionary alloc] init]; [requestDictionary setObject:[NSString stringWithString:@"12"] forKey:@"foo"]; [requestDictionary setObject:[NSString stringWithString@"*"] forKey:@"bar"]; NSString *theBodyString = requestDictionary.JSONRepresentation; NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding]; [请求设置HTTPBody:theBodyData]; [[NSURLConnection alloc] initWithRequest:request delegate:self];当我在 Django 视图中读取此请求时,调试器显示它获取了整个 JSON 字符串并将其作为 POST QueryDict 的第一个键:
POST QueryDict: QueryDict: {u'{"foo":"12","bar":"*"}': [u'']}> 错误无法解析变量我可以读取第一个密钥,然后使用 JSON 作为 hack 重新解析。但是为什么没有正确发送 JSON 字符串?
【问题讨论】:
标签: iphone objective-c django json