1

我正在尝试在 post 请求中将 JSON 数据发送到引发错误的服务器。我正在创建以下格式的数据

      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

4

2 回答 2

0

在我看来,您在这一行中序列化了错误的对象:

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

为什么要序列化包含字典的数组?当然,您应该只是序列化字典本身。

NSData *jsData = [NSJSONSerialization dataWithJSONObject:o3
  options:NSJSONWritingPrettyPrinted error:nil];
于 2013-05-30T14:49:01.163 回答
0

我想知道您为什么要执行以下操作:

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

行得通吗?

于 2013-05-30T16:31:46.770 回答