美文网首页ios
iOS post请求传JSON时,JSON里有\去不掉

iOS post请求传JSON时,JSON里有\去不掉

作者: 童宁_深圳_iOS | 来源:发表于2017-03-20 15:18 被阅读259次

NSMutableDictionary *params = [NSMutableDictionary dictionary];  //我们要上传的子参数字典

params[@"xxx"] = @"xxx";  //字典内容设置

//重点来了!

NSError *error; 

NSData * jsonData  =  [NSJSONSerialization dataWithJSONObject: params options:NSJSONWritingPrettyPrinted error:&error];  //第一步,字典转数据

NSString *jsonString = [[NSString alloc]initWithData: jsonData encoding:NSUTF8StringEncoding];  //第二部,数据转JSON

NSString *outerJson = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];  //第三步,处理JSON,去掉“\”转义

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];  //定义新字典存参

dictionary[@"data"] = outerJson;  //将处理好的JSON存入key中

附加:

    post需求:参数名称-data

                    参数结构:{

                                        "xxx": "xxxxxx",

                                        "xxxx": [       

                                                        {

                                                               "xxxx": "xxxx",

                                                               "xxxx": "xxxx"

                                                        }

                                                 ]

                                      }

相关文章

网友评论

    本文标题:iOS post请求传JSON时,JSON里有\去不掉

    本文链接:https://www.haomeiwen.com/subject/vitinttx.html