字典转为json的方法

来源:互联网 发布:seo与竞价排名的关系 编辑:程序博客网 时间:2024/05/17 23:47

用处:把用户输入的表单值放入字典,字典在转json传

方法:

+ (NSString*)jsonFromDictionary:(NSDictionary*)dict

{

    NSData* jsonData = [NSJSONSerializationdataWithJSONObject:dict options:0error:nil];

    return [[NSStringalloc] initWithBytes:[jsonData bytes] length:[jsonData length]encoding:NSUTF8StringEncoding];

}


用法:


    NSDictionary* curBank =cgyhArr[currentCGXYView.tag];

    NSMutableDictionary* dictcg = [[NSMutableDictionaryalloc] init];

    

    [dictcg setValue:_inputValues[2]forKey:@"YHZH"];

    [dictcg setValue:_inputValues[3]forKey:@"YHMM"];

    [dictcg setValue:@"1"forKey:@"ZZHBZ"];//指定行币种

    [dictcg setValue:@"1"forKey:@"BZ"];//币种

    [dictcg setValue:[curBank valueForKey:@"YHDM" ] forKey:@"YHDM"];//银行代码

    

    NSMutableDictionary* dictxy = [[NSMutableDictionaryalloc] init];

    [dictxy setValue:[curBank valueForKey:@"XYID"forKey:@"HTXY"];//合同协议

    [dictxy setValue:@""forKey:@"XYBH"];//协议编号

    [dictxy setValue:qmlsh forKey:@"QMLSH"];//签名流水号

    

    NSMutableDictionary* json = [[NSMutableDictionaryalloc] init];

    [json setValue:@[dictcg]forKey:@"CGYHSTR"];

    [json setValue:@[dictxy]forKey:@"CGXYSTR"];

    if (_isReAssign) {

        [json setValue:[_khztvalueForKey:@"bdid"]forKey:@"BDID"];

    }

    return [SSUtilityjsonFromDictionary:json];




0 0