iOS开发:字典(NSDictionary)和数组( NSArray)转换成字符串

版权声明:转载请注明出处:http://blog.csdn.net/kevindongkun https://blog.csdn.net/Kevindongkun/article/details/51741669

概述:
在项目开发中,总是与服务器进行着接口的交互,传参是不可缺少的一部分。后台返回的j son数据我们可以很好的解析使用,但是我们如何向后台发送j son数据的参数呢?要知道,一般服务器是不允许传对象的。以下是在项目中遇到的实际问题,在此总结一下,供大家交流。

一. 字典转字符串:
1。商品评论需求:对购买的产品进行评论,评论时需要上传图片,而且对本次购买的所有产品需要评论。例如:在张三家买了一瓶水,一个面包。评论时需要对水和面包分别评论,并把评论内容一起传给后台。数据如下:

    NSDictionary *jsonDic;
    jsonDic = @{
    //店铺信息
        @"orderId" : _orderId,   
        @"goodsId" : _goodsId, 
        @"typeCode" : @"app",
        @"isAnonymity" : isAnonymity, 
        //评论内容
        @"comm" : @[
            @{
            @"commentMainId" : _orderGoodsId1,//水的ID
            @"t1" : @"10",  //5星好评
            @"commentContent": text1,  // 内容
            @"comPictures" : comPicturesArr //评论水的图片数组
             },
             @{
            @"commentMainId" : _orderGoodsId2,
            @"t1" : @"9", 
            @"commentContent": text2, 
            @"comPictures" : comPicturesArr2
             }
                    ]
                }

但是怎么将该字典传到后台服务器,传参只能是字符串,不能直接传对象格式。
2。解决方法:
需要将改字典利用NSJSON转化成字符串;

 NSString  *jsonString = [jsonDic JSONString];

二. 数组转字符串:
1。本地上传资源到服务器:上传多个资源到服务器,数据如下:

上传资源需要利用form形式将文件路径转化成字符串(上传头像的URL,上传文件的文件URL)
 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            [manager POST:[NSString stringWithFormat:@"%@%@", SERVER_URL, UploadReource] parameters:@{@"fileType" : fileType}  constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                 //将获取的文件路径(NSSting)转化成数据流(NSData):
  NSData *fileData = [filePath dataUsingEncoding:NSUTF8StringEncoding];
                //以文件方式上传
                [formData appendPartWithFileData:fileData name:@"myFile" fileName:@"file" mimeType:@"resourceFile"];

            } success:^(AFHTTPRequestOperation *operation, id responseObject) {

                if ([[responseObject objectForKey:@"res_type"] isEqualToString:@"success"]) {
            //上传成功
                    NSString *urlString = [responseObject objectForKey:@"lyrss_msg"];

       /*********************************************/
  NSArray *fileArray =     @[
                    @{
                    @"keyword" : @"资源名称1",
                    @"videoFile" : urlString1
                    }, 
                    @{
                    @"keyword" : @"资源名称2",
                    @"videoFile" : urlString2
                    }
                        ];

 //后台服务器同样不接受对象,必须把数组转化成字符串传给后台  
 (1).先讲数组转化成NSData数据
                    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:fileArray options:NSJSONWritingPrettyPrinted error:nil];

  (2).在将NSData转化成字符串     
  NSString *fileString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

  (3).请求接口,其他业务流程             
}else{
//文件上传失败
      }

  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  //接口请求失败
          }];

猜你喜欢

转载自blog.csdn.net/Kevindongkun/article/details/51741669