2011-05-10 59 views
3

发送JSON数组我需要通过POST发送以下JSON阵列到我们的服务器:iPhone通过POST

task:{"id":"123","list":"456","done":1,"done_date":1305016383} 

我与JSON库试过,但我莫名其妙地愚蠢的用它。我甚至试图自己建立POST-String,但也失败了:

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'ed POST字符串对我来说已经足够了:)

+0

[JSON数组格式]可能的重复(http://stackoverflow.com/questions/7055475/json-array-format) – 2012-01-16 20:20:52

回答

0

我想问题不在于方法,而在于你想要发布的字符串。试试这个:

NSString *post = @"\"task\":{\"id\":\"123\",\"list\":\"456\",\"done\":1,\"done_date\":1305016383}"; 

换句话说:用引号进行实验。

你使用的是什么JSON库?

+0

我正在使用这个JSON库:http: //stig.github.com/json-framework您的示例在将字符串的开头更改为task = {\“id \”之后工作: – Sebastian 2011-05-10 13:08:40

+0

JSON框架是一个众所周知的库。虽然我使用JSONkit,但我相信JSON框架可以正确地完成这项工作并返回格式良好的字符串。因此,问题出现在服务器端或传递给JSON框架的对象中。 – adubr 2011-05-10 13:33:00

1

所以这可能是也可能不是你问的问题,但你的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]]; 
... 

祝你好运!

+0

工作就像一个魅力,并解决我们的问题,花了我们几个小时 – chings228 2017-03-28 09:32:36

1

这是我如何处理它:

-(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是一个字符串,它看起来像这样 { “用户”:1337, “称号”: “有些题”, “itemKeys”:1 ,1,1,1,1]}

+1

没有可见@interface为'NSDictionary'声明选择器'JSONRepresentation'''你见过这个错误吗? – 2012-09-04 13:57:38

+0

嘿,你能弄清楚那个错误吗?它为我做了同样的事情。 – gdubs 2013-05-28 05:43:15