0

I'm posting NSDictionary to server and get NSLog like this :

{"User":"abc@gmail.com","cartItems":[{"productName":"Apple 5s","Qty":1,"price":"1000"}],"userDiscounts":["0001"]}

but the problem is when i am checking this data in server side :

{ '{"User":"abc@gmail.com","cartItems":': { '{"productName":"Apple 4s","Qty":1,"price":"1000"}],"userDiscounts"': { '"0001"]': '' } } }

I'mean, getting '{ and }' on server side. What is the problem in both the json dictionary.

This is my method:

 // 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]);
Amar
  • 13,202
  • 7
  • 53
  • 71
  • 2
    possible duplicate of [Post data in Objective C using Json](http://stackoverflow.com/questions/9883081/post-data-in-objective-c-using-json) – nielsbot Nov 12 '13 at 06:09

1 Answers1

0

Use wrapper of NSURLConnection i.e AFNetworking https://github.com/AFNetworking/AFNetworking. As per your problem

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

hope it helps you.

Muhammad Saad Ansari
  • 1,748
  • 3
  • 13
  • 20