iPhone通过POST发送Json数组

发布于 2024-11-06 08:13:08 字数 737 浏览 0 评论 0原文

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

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 字符串对我来说就足够了:)

I need to send the following JSON array via POST to our server:

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

I tried with the JSON library, but I was somehow to stupid to use it. I even tried to build up the POST-String by myself, but also failed:

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]];
....

Can you please help me? The json’ed POST string would be even enough for me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

無心 2024-11-13 08:13:08

因此,这可能是也可能不是您要问的问题,但您的 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]];
...

祝你好运!

So this may or may not be the question you are asking, but your JSON string is not formed correctly. An array of "task" in JSON format would look like this:

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

I was just wresting with a similar situation posting to a PHP server and I couldn't find any questions about it online, but this is what I would've had to do if I were posting the same data:

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]];
...

Good luck!

扎心 2024-11-13 08:13:08

这就是我处理它的方式:

-(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":"某些标题","itemKeys":[1,1,1,1,1]}

This is how I handle It:

-(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;
}

My json is a string that looks like this
{"user":1337,"title":"Some title","itemKeys":[1,1,1,1,1]}

从来不烧饼 2024-11-13 08:13:08

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

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

换句话说:尝试使用引号。

您使用什么 JSON 库?

I suppose the problem is not with the methods, but with the string you're trying to post. Try this one:

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

In other words: experiment with the quotation marks.

What JSON library are you using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文