2011-05-10 58 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