我对JSON
的格式要求如下。
{
"first_name" : "XYZ",
"last_name" : "ABC"
}
我在
NSString
中有值。NSString strFName = @"XYZ";
NSString strLName = @"ABC";
NSString strKeyFN = @"first_name";
NSString strKeyLN = @"last_name";
我用
NSMutableDictionary
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
[dict setObject:strFName forKey:strKeyFN];
[dict setObject:strLName forKey:strKeyLN];
然后输出是
{
first_name = XYZ,
last_name = ABC
}
所以我不希望“=”分隔键和值,而是希望“:”分隔键和值
我已经完成了stack overflow questions的大部分工作,但无助于仅在输出中获取“=“
那么请帮忙吗?
最佳答案
这是你的答案:
NSString *strFName = @"XYZ";
NSString *strLName = @"ABC";
NSInteger number = 15;
NSString *strKeyFN = @"first_name";
NSString *strKeyLN = @"last_name";
NSString *numValue = @"Number";
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:strFName forKey:strKeyFN];
[dic setObject:strLName forKey:strKeyLN];
[dic setObject:[NSNumber numberWithInt:number] forKey:numValue];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSArray arrayWithObject:dic] options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON %@",jsonString);