在发送请求中的iphone中发送json数据的问题iphone

问题描述:

我正试图发送发送错误的发送请求到服务器的JSON数据。我在下面的格式创建数据在发送请求中的iphone中发送json数据的问题iphone

 NSDictionary *o1 = [[NSMutableDictionary alloc] init]; 
     [o1 setValue:@"51" forKey:@"merchantProductId"]; 
     [o1 setValue:@"Big Paulie" forKey:@"name"]; 
     [o1 setValue:@"1" forKey:@"quantity"]; 
     NSDictionary *o2 = [[NSMutableDictionary alloc] init]; 
     [o2 setValue:@"52" forKey:@"merchantProductId"]; 
     [o2 setValue:@"Paulie" forKey:@"name"]; 
     [o2 setValue:@"10" forKey:@"quantity"]; 

     NSMutableArray *pizzas = [NSMutableArray arrayWithObjects:o1, o2, nil]; 
     NSDictionary *o3 = [[NSMutableDictionary alloc] init]; 
     [o3 setValue:@"3" forKey:@"merchantId"]; 
     [o3 setValue:pizzas forKey:@"pizzas"]; 
     NSMutableArray *orderArray = [NSMutableArray arrayWithObjects:o3, nil]; 
     NSData *jsData = [NSJSONSerialization dataWithJSONObject:orderArray options:NSJSONWritingPrettyPrinted error:nil]; 
NSString *data = [NSString stringWithFormat:@"data=%@",[[NSString alloc] initWithData:jsData                     encoding:NSUTF8StringEncoding]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:61.12.124.234:60/upload_image/phoneTesting.php"]]]; 
    [request setHTTPMethod:@"POST"]; 
    // [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 

    NSHTTPURLResponse* urlResponse = nil; 
    error = [[NSError alloc] init]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    NSLog(@"Response: %@", result); 

服务器的,我需要的数据应该出现像

 { 
    "pizzas" : [ 
     { 
     "quantity" : "1", 
     "merchantProductId" : "51", 
     "name" : "Big Paulie" 
     }, 
     { 
     "quantity" : "10", 
     "merchantProductId" : "52", 
     "name" : "Paulie" 
     } 
    ], 
    "merchantId" : "3" 
    } 

但我正在逐渐

(
    [data] => [ 
    { 
    "pizzas" : [ 
     { 
     "quantity" : "1", 
     "merchantProductId" : "51", 
     "name" : "Big Paulie" 
     }, 
     { 
     "quantity" : "10", 
     "merchantProductId" : "52", 
     "name" : "Paulie" 
     } 
    ], 
    "merchantId" : "3" 
    } 
] 
) 

,当我试图只送jsData它不会发送任何与请求相关的东西。

请建议我如何发送发布请求中唯一的json数据。

我使用xcode4.5

+0

'postData'初始化在哪里? – Mar0ux

+0

上面有几个值,没有明显的初始化。目前还不清楚jsData如何将其纳入请求。请提供完整的代码。 –

我不知道为什么你做下面一行:

NSString *data = [NSString stringWithFormat:@"data=%@", 
    [[NSString alloc] initWithData:jsData encoding:NSUTF8StringEncoding]]; 

如果这是一个GET请求,那么你需要一个键值配对,但你只是发送POST数据,它不需要钥匙。

我认为这就是为什么你会得到那个白眼[data] = entry。如果你做到了:

NSString *data = [NSString stringWithFormat:@"%@", 
    [[NSString alloc] initWithData:jsData encoding:NSUTF8StringEncoding]]; 

它工作吗?

+0

看不出如何工作,因为“数据”显然永远不会被使用。 –

+0

是的,你是对的。如果数据真的是postdata,那么我可能在这里有一些东西。看起来他认为他需要使用POST有效负载的GET语法。 –

它看起来对我像你序列化在此行错了对象:

NSData *jsData = [NSJSONSerialization dataWithJSONObject:orderArray 
    options:NSJSONWritingPrettyPrinted error:nil]; 

你为什么序列化包含字典中的阵列?当然,你应该只是序列化字典本身。

NSData *jsData = [NSJSONSerialization dataWithJSONObject:o3 
    options:NSJSONWritingPrettyPrinted error:nil];