标签:
两种json字符串拼接的方法如下:
+ (NSString *)UIUtilsFomateJsonWithDictionary:(NSDictionary *)dic {
NSArray *keys = [dic allKeys];
NSString *string = [NSString string];
for (NSString *key in keys) {
NSString *value = [dic objectForKey:key];
value = [NSString stringWithFormat:@"\"%@\"",value];
NSString *newkey = [NSString stringWithFormat:@"\"%@\"",key];
if (!string.length) {
string = [NSString stringWithFormat:@"%@:%@}",newkey,value];
}else {
string = [NSString stringWithFormat:@"%@:%@,%@",newkey,value,string];
}
}
string = [NSString stringWithFormat:@"{%@",string];
return string;
}
+ (NSString *)UIUtilsFomateJsonArrWithArray:(NSArray *)array {
NSString *string = [NSString string];
for (int j = 0; j<array.count; j++) {
NSDictionary *dic = array[j];
NSArray *keys = [dic allKeys];
for (int i = 0; i<keys.count; i++) {
NSString *key = keys[i];
NSString *value = [dic objectForKey:key];
value = [NSString stringWithFormat:@"\"%@\"",value];
key = [NSString stringWithFormat:@"\"%@\"",key];
if (!string.length) {
string = [NSString stringWithFormat:@"%@:%@}",key,value];
}else if(i == 0){
string = [NSString stringWithFormat:@"%@:%@}%@",key,value,string];
}else {
string = [NSString stringWithFormat:@"%@:%@,%@",key,value,string];
}
}
if (j != array.count-1) {
string = [NSString stringWithFormat:@",{%@",string];
}else {
string = [NSString stringWithFormat:@"[{%@]",string];
}
}
return string;
}
调用:
第一种直接传入@"key":@"value"}即可,
第二种array里面对象需要是字典:NSArray *array = @[@{@"key":@"value"}];
标签:
原文地址:http://www.cnblogs.com/leconglee/p/4848790.html