iPhone通过POST发送Json数组
我需要通过POST向我们的服务器发送以下JSON数组:iPhone通过POST发送Json数组,iphone,objective-c,json,post,Iphone,Objective C,Json,Post,我需要通过POST向我们的服务器发送以下JSON数组: task:{"id":"123","list":"456","done":1,"done_date":1305016383} 我试过使用JSON库,但不知何故,我使用它太愚蠢了。我甚至试着自己建立POST字符串,但也失败了: NSString *post = @"task='{id:123,list:456,done:1,done_date:1305016383}'"; NSMutableURLRequest *request=[NSM
task:{"id":"123","list":"456","done":1,"done_date":1305016383}
我试过使用JSON库,但不知何故,我使用它太愚蠢了。我甚至试着自己建立POST字符串,但也失败了:
NSString *post = @"task='{id:123,list:456,done:1,done_date:1305016383}'";
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
....
你能帮帮我吗?json格式的POST字符串对我来说已经足够了:)我想问题不在于方法,而在于您试图发布的字符串。试试这个:
NSString *post = @"\"task\":{\"id\":\"123\",\"list\":\"456\",\"done\":1,\"done_date\":1305016383}";
换句话说:用引号做实验
您使用的是什么JSON库?所以这可能是您要问的问题,也可能不是,但您的JSON字符串格式不正确。JSON格式的“任务”数组如下所示:
NSString *post = @"{"task":[{"id":"123","list":"456","done":1,"done_date":1305016383}]}";
我刚刚在一个PHP服务器上发布了一个类似的情况,我在网上找不到任何关于它的问题,但如果我发布相同的数据,我就必须这样做:
NSString *post = @"task[0][id]=123&task[0][list]=456&task[0][done]=1&task[0][done_date]=1305016383&";
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
...
祝你好运 我就是这样处理的:
-(void)imageFactory:(NSDictionary*)postJSON
{
// Convert the JSON string to Data to be sent.
NSData* postData = [[postJSON JSONRepresentation] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[URLManager imageFactory]]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
// IMPORTANT MAKE SURE YOU ADD THIS LINE SO THE SERVER KNOWS WHAT ITS GETTING
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
// Add some nice handlers so you know what you got from the server
NSURLResponse* response;
NSHTTPURLResponse* httpResponse;
NSError* error;
NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* stringResponse = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
httpResponse = (NSHTTPURLResponse*) response;
int statuscode = [httpResponse statusCode];
if (statuscode == 200)
{
log4Debug(@"ImageFactory Response Successful, Retrieving image");
// Handle the response here if needed
}
else
{
log4Error(@"ImageFactory Response Failed: %@", stringResponse);
// Show some form of alert here if needed
}
// release all objects saved to memory
[request release];
request = nil;
[stringResponse release];
stringResponse = nil;
}
我的json是一个如下所示的字符串
{“user”:1337,“title”:“Some title”,“itemKeys”:[1,1,1,1,1]}我正在使用这个JSON库:在我将字符串的开头更改为task={“id”之后,您的示例就成功了:JSON framework是一个众所周知的库。虽然我使用JSONkit,但我相信JSON framework能够正确地完成任务并返回格式良好的字符串。因此,问题要么在服务器端,要么在传递给JSON framework的对象中。可能重复的
No visible@interface for'NSDictionary'声明选择器'JSONReprespen'你看到这个错误了吗?嘿,你能找出那个错误吗?它对我也有同样的作用。好好工作,解决我们花了几个小时的问题