• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - JSON POST Web 服务中的多个 Json 对象和 3 个字符串

[复制链接]
菜鸟教程小白 发表于 2022-12-13 14:27:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

 NSURL *url=[NSURL URLWithString"my url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod"OST"]; 
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":"[email protected]","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"}}



Best Answer-推荐答案


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

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

关于ios - JSON POST Web 服务中的多个 Json 对象和 3 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35408138/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap