如何将两个字典值放入一个字符串中?
How to get two dictionary values into a single string?
我有 JSON 个回复
q13 = {
multiple = multiple;
restrict = all;
title1 = "Board Certification:";
title2 = "Exp Date:";
type = "double_text";
value1 = (
"Test Certificate"
);
value2 = (
"12/14/27"
);
};
现在,在 JSON 响应中,我只需要 value1 和 value2 的值,这两个值必须存储在以逗号 (,) 分隔的单个字符串中。
怎么办?
只是我创建了两个 NSmutabledictionary 并进入一个字符串。你有任何疑问让我知道我解释一下。
NSMutableDictionary *Dict_Value;
NSMutableDictionary *Dict_Value1;
Dict_Value = [[NSMutableDictionary alloc] init];
[Dict_Value setObject:@"FirstDict" forKey:@"One"];
[Dict_Value setObject:@"FirstDict" forKey:@"Two"];
[Dict_Value setObject:@"FirstDict" forKey:@"Three"];
[Dict_Value setObject:@"FirstDict" forKey:@"Four"];
Dict_Value1 = [[NSMutableDictionary alloc] init];
[Dict_Value1 setObject:@"2Multiple1" forKey:@"2One"];
[Dict_Value1 setObject:@"2Multiple2" forKey:@"2Two"];
[Dict_Value1 setObject:@"2Multiple3" forKey:@"2Three"];
[Dict_Value1 setObject:@"2Multiple4" forKey:@"2Four"];
NSString *string= [NSString stringWithFormat:@"Dict1 %@, Dict2 %@",[Dict_Value valueForKey:@"Value1"],[Dict_Value1 valueForKey:@"Value2"]];
请考虑以下是您将在 JSON 中获得的 dic:
NSMutableDictionary *dicMain = [NSMutableDictionary new];
dicMain[@"multiple"] = @"multiple";
dicMain[@"restrict"] = @"all";
dicMain[@"title1"] = @"Board Certification:";
dicMain[@"title2"] = @"Exp Date:";
dicMain[@"type"] = @"double_text";
dicMain[@"value1"] = [NSArray arrayWithObject:@"Test Certificate"];
dicMain[@"value2"] = [NSArray arrayWithObject:@"12/14/27"];
这是将值放入一个字符串的逻辑:
NSArray *aryValue1 = [dicMain valueForKey:@"value1"];
NSArray *aryValue2 = [dicMain valueForKey:@"value2"];
NSString *strFinalString = [NSString stringWithFormat:@"%@ %@",[aryValue1 componentsJoinedByString:@" "],[aryValue2 componentsJoinedByString:@" "]];
NSLog(@"%@",strFinalString);
这样做很简单。
如果您的 both 数组中有多个值。
NSDictionary *dictionary =[your_Json_Object valueForKey:@"q13"];
NSArray *value1Array = [dictionary valueForKey:@"value1"];
NSArray *value2Array = [dictionary valueForKey:@"value2"];
NSMutableString *combinedString = [NSMutableString new];
[value1Array enumerateObjectsUsingBlock:^(NSString *str1, NSUInteger idx, BOOL *stop) {
NSString * innerCombinedString = [NSString stringWithFormat:@"%@ %@",str1,[value2Array objectAtIndex:idx]];
[combinedString appendString:innerCombinedString];
if (idx != value1Array.count) {
[combinedString appendString:@","];
}
}];
我有 JSON 个回复
q13 = {
multiple = multiple;
restrict = all;
title1 = "Board Certification:";
title2 = "Exp Date:";
type = "double_text";
value1 = (
"Test Certificate"
);
value2 = (
"12/14/27"
);
};
现在,在 JSON 响应中,我只需要 value1 和 value2 的值,这两个值必须存储在以逗号 (,) 分隔的单个字符串中。
怎么办?
只是我创建了两个 NSmutabledictionary 并进入一个字符串。你有任何疑问让我知道我解释一下。
NSMutableDictionary *Dict_Value;
NSMutableDictionary *Dict_Value1;
Dict_Value = [[NSMutableDictionary alloc] init];
[Dict_Value setObject:@"FirstDict" forKey:@"One"];
[Dict_Value setObject:@"FirstDict" forKey:@"Two"];
[Dict_Value setObject:@"FirstDict" forKey:@"Three"];
[Dict_Value setObject:@"FirstDict" forKey:@"Four"];
Dict_Value1 = [[NSMutableDictionary alloc] init];
[Dict_Value1 setObject:@"2Multiple1" forKey:@"2One"];
[Dict_Value1 setObject:@"2Multiple2" forKey:@"2Two"];
[Dict_Value1 setObject:@"2Multiple3" forKey:@"2Three"];
[Dict_Value1 setObject:@"2Multiple4" forKey:@"2Four"];
NSString *string= [NSString stringWithFormat:@"Dict1 %@, Dict2 %@",[Dict_Value valueForKey:@"Value1"],[Dict_Value1 valueForKey:@"Value2"]];
请考虑以下是您将在 JSON 中获得的 dic:
NSMutableDictionary *dicMain = [NSMutableDictionary new];
dicMain[@"multiple"] = @"multiple";
dicMain[@"restrict"] = @"all";
dicMain[@"title1"] = @"Board Certification:";
dicMain[@"title2"] = @"Exp Date:";
dicMain[@"type"] = @"double_text";
dicMain[@"value1"] = [NSArray arrayWithObject:@"Test Certificate"];
dicMain[@"value2"] = [NSArray arrayWithObject:@"12/14/27"];
这是将值放入一个字符串的逻辑:
NSArray *aryValue1 = [dicMain valueForKey:@"value1"];
NSArray *aryValue2 = [dicMain valueForKey:@"value2"];
NSString *strFinalString = [NSString stringWithFormat:@"%@ %@",[aryValue1 componentsJoinedByString:@" "],[aryValue2 componentsJoinedByString:@" "]];
NSLog(@"%@",strFinalString);
这样做很简单。 如果您的 both 数组中有多个值。
NSDictionary *dictionary =[your_Json_Object valueForKey:@"q13"];
NSArray *value1Array = [dictionary valueForKey:@"value1"];
NSArray *value2Array = [dictionary valueForKey:@"value2"];
NSMutableString *combinedString = [NSMutableString new];
[value1Array enumerateObjectsUsingBlock:^(NSString *str1, NSUInteger idx, BOOL *stop) {
NSString * innerCombinedString = [NSString stringWithFormat:@"%@ %@",str1,[value2Array objectAtIndex:idx]];
[combinedString appendString:innerCombinedString];
if (idx != value1Array.count) {
[combinedString appendString:@","];
}
}];