Json 对象和 JSON POST 网络服务中的 3 个字符串

Multiple Json objects and 3 string in JSON POST web service

我正在研究 Post 方法。我有 3 个 json 和 3 个字符串。我想将它们用于 post 方法。 我尝试了很多解决方案,但没有解决。 order_item_details、order_details、device_info 有 json 个对象

 NSURL *url=[NSURL URLWithString:@"my url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"]; 
NSMutableData *jsonData = [NSMutableData data];

// add params (all params are strings)
[jsonData appendData:[[NSString stringWithFormat:@"{order_item_details=%@", orderItemDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",order_details=%@", orderDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",transaction_id=%@", transID] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",order_unique_id=%@", unique] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",user_id=%@", userid] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@",device_info=%@}", deviceInfo] dataUsingEncoding:NSUTF8StringEncoding]];
[request setURL:url];
[request setValue:[NSString stringWithFormat:@"%d", jsonData.length] forHTTPHeaderField:@"Content-Length"];
//            //HTTP Authentication
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"admin", @"1234"];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];

NSString *authValue = [authData base64Encoding];

[request setValue:[NSString stringWithFormat:@"Basic %@",authValue] forHTTPHeaderField:@"Authorization"];

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if ([response statusCode] >= 200 && [response statusCode] < 300)
{
    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

    SBJsonParser *jsonParser = [SBJsonParser new];
    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];

    NSLog(@"%@",jsonData);
}

作为回应,我得到了空值。我有 json 如下所示

 {order_item_details=     [{"item_amount":"500.00","item_price":"100.00","item_size":"M","page_name":"Billi","quantity":"5","fk_image_id":880},{"item_amount":"250.00","item_price":"50.00","item_size":"S","page_name":"Billi","quantity":"5","fk_image_id":880}], 

 order_details=[{"email":"my@email.com","mobile_order_date":"2016-02-11 11:44:16","orderUniqueId":"3204a5eb8be171a51057678233268718302","order_price":"750.00","order_total_amount":"800.00","order_type":"COD","paypal_transaction_id":"","service_charges":"40.00","service_percentage":"5.00","shipping_address":"asad","shipping_cost":"50.0","shipping_name":"asad","shipping_phone":"123456","fk_user_id":141,"no_of_items":2,"order_id":1}], 

 transaction_id=, 
 order_unique_id=3204a5eb8be171a51057678233268718302, 
 user_id=141,
 device_info={"device":"klimtwifi","device_model":"SM-T700","device_brand":"samsung","device_id":"LRX22G","device_serial":"3204a5eb8be171a5","device_manufacturer":"samsung","device_product":"klimtwifixx"}}

创建字典如下:-

NSDictionary *parameterDictionary = @{@"order_item_details":orderItemDetails, @"order_details":orderDetails, @"transaction_id":transID, @"order_unique_id":unique, @"user_id":userid, @"device_info":deviceInfo};  //Modify the dictionary as per your need

NSData *requestData = [NSJSONSerialization dataWithJSONObject:parameterDictionary options:NSJSONWritingPrettyPrinted error:nil];
[request setHTTPBody: requestData];

希望对您有所帮助!

我已经解决了这个问题,问题是服务器将数据视为一个对象。我已更改为以下代码

NSMutableData *jsonData = [NSMutableData data];
[jsonData appendData:[[NSString stringWithFormat:@"order_item_details=%@&", orderItemDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"order_details=%@&", orderDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"transaction_id=%@&", transID] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"order_unique_id=%@&", unique] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"user_id=%@&", userid] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"device_info=%@", deviceInfo] dataUsingEncoding:NSUTF8StringEncoding]];
[request setURL:url];