我将NSDictionary发布到服务器并获取NSLog,如下所示:
{"User":"abc@gmail.com","cartItems":[{"productName":"Apple 5s","Qty":1,"price":"1000"}],"userDiscounts":["0001"]}
但问题是当我在服务器端检查这些数据时:
{ '{"User":"abc@gmail.com","cartItems":': { '{"productName":"Apple 4s","Qty":1,"price":"1000"}],"userDiscounts"': { '"0001"]': '' } } }
我想,在服务器端获得'{ and }'
。
这两个json字典都有什么问题。
这是我的方法:
// Convert object to data, cartDictionary holding data.
NSData* postData = [NSJSONSerialization dataWithJSONObject:cartDictionary options:kNilOptions error:&error];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:combineProductUrl]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
// print json:
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:postData
encoding:NSUTF8StringEncoding]);
答案 0 :(得分:0)
使用NSURLConnection的包装器,即AFNetworking https://github.com/AFNetworking/AFNetworking。根据你的问题
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: deviceCode ,@"Key 1", Value 1 , @"Key 2", Value 2 , nil];
NSLog(@"Parameter %@",parameters);
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString:@"http://yourbaseURL/"]];
[client setDefaultHeader:@"contentType" value:@"application/json; charset=utf-8"];
client.parameterEncoding = AFJSONParameterEncoding;
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"yourPostURL" parameters:parameters];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSLog(@"response %@",JSON);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"request %@",[error localizedDescription]);
}];
[operation start];
希望它可以帮到你。