nsdictionary ,nsarray 和 json 字符串 的转换

来源:互联网 发布:在淘宝怎么买机票 编辑:程序博客网 时间:2024/04/29 09:15


一,提交数据: foudation 类型转 json 字符串


                                         

1.字符拼接方法


NSMutableString *listStr = [[NSMutableString alloc] initWithString:@"["];        for (NSString *title in _choiceArray) {                NSString *dicString = [NSString stringWithFormat:@"{\"id\":\"\",\"title\":\"%@\"}",title];        [listStr appendFormat:@"%@,",dicString];    }    [listStr replaceCharactersInRange:NSMakeRange(listStr.length-1,1) withString:@"]"];        [postDict setValue:listStr forKey:@"list"];


2.使用 NSJSONSerialization


NSMutableArray *listArr = [[NSMutableArray alloc] init];    for (NSString *title in _choiceArray) {                NSDictionary *dic = @{@"id":@"",@"title":title};        [listArr addObject:dic];    }        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:listArr                                                       options:NSJSONWritingPrettyPrinted                                                         error:nil];    NSString *jsonString = [[NSString alloc] initWithData:jsonData                                                 encoding:NSUTF8StringEncoding];        [postDict setValue:jsonString forKey:@"list"];

  http json URL: &q_type=1&list=[{"id":"","title":"韩国"},{"id":"","title":"高富帅"}]&acc_id=58159&updateType=1



0 0
原创粉丝点击