nsdictionary ,nsarray 和 json 字符串 的轉換


一,提交數據: 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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章