NSArray *valueArray = [@"val1 val2 val3" componentsSeparatedByString:@" "];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *each in [@"KeyA KeyB KeyC" componentsSeparatedByString:@" "])
[dict setObject:valueArray forKey:each];
if ([NSJSONSerialization isValidJSONObject:dict])
{
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self log:@"Result: %@", result];
}
打印结果
Result: {"KeyA":["val1","val2","val3"],"KeyC":["val1","val2","val3"],"KeyB":["val1","val2","val3"]}