Using JSON-Framework to prepare json object to send via Objective-C HTTP request

I successfully use json-framework to create GET HttpRequests. Does anyone have some code to prepare a json object and execute an HTTP POST request? If so, can you share some objective-c code example. Thanks

+3
source share
3 answers

Take a look at the open source project hosted in google code.

Description:

This structure implements a strict parser and JSON generator in Objective-C.

, JSON.h. JSON. Objective-C API .

+3

TwitterHelper.m CS 193P " Presence3Files.zip ".

, , , .

+1

Use the following code to create a mail request using a JSON data object.

   self.responseData=[NSMutableData data];

   NSURL *url = [NSURL URLWithString:@"http://dev.iworklab.com/myProject/index.php"];

   NSString *jsonRequest = [NSString stringWithFormat:@"{\"method\":\"changePassword\",\"customer_id\":\"%@\",\"old_password\":\"%@\",\"new_password\":\"%@\",\"con_password\":\"%@\"}",customerID,oldPasswordText.text,newPasswordText.text,confirmPasswordText.text];

   jsonRequest = [NSString stringWithFormat:@"&json_data=%@",jsonRequest];

   NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

   [request setHTTPMethod:@"POST"];
   [request setHTTPBody: json_data];

   [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
   [request setValue:[NSString stringWithFormat:@"%d", [json_data length]] forHTTPHeaderField:@"Content-Length"];
   [request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
                      dataUsingEncoding:NSUTF8StringEncoding
                      allowLossyConversion:YES]];

    passwordConnection = [NSURLConnection connectionWithRequest:request delegate:self];
0
source

Source: https://habr.com/ru/post/1715124/


All Articles