使用 NSString 作为键和值创建 JSON 格式

Create JSON format using NSString as a key and values

我有 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 但没有帮助只在输出中得到“=”

请帮忙?

你在你的 NSDictionary 之后写这段代码

NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonstr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

试试这个:-

NSString *cleanedString1 =[strFName stringByReplacingOccurrencesOfString:@"/"" withString:@""];
NSString *cleanedString2 =[strLName stringByReplacingOccurrencesOfString:@"/"" withString:@""];

NSDictionary *Dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                cleanedString1, strKeyFN,
                                cleanedString2, strKeyLN,nil];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:Dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);

这是你的答案:

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);

NSString *strFName = @"XYZ";
NSString *strLName = @"ABC";
NSString *strKeyFN = @"first_name";
NSString *strKeyLN = @"last_name";
NSDictionary *ictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      strKeyFN, strFName,strKeyLN, strLName,nil];

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"dictionary as string:%@", jsonString);

您必须将 NSMutableDictionary 转换为 NSData

然后将NSData转换成你想要的json字符串

NSString *strFName = @"XYZ";
 NSString *strLName = @"ABC";
 NSString *strKeyFN = @"first_name";
 NSString *strKeyLN = @"last_name";
 NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
 [dict setObject:strFName forKey:strKeyFN];
 [dict setObject:strLName forKey:strKeyLN];

NSData *data = [NSJSONSerialization dataWithJSONObject:dict    options:NSJSONWritingPrettyPrinted error:nil];

NSString *jasonString= [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSString *strFName = @"ABC";
NSString *strLName = @"XYZ";

NSString *strKeyFN = @"last_name";
NSString *strKeyLN = @"first_name";

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

[dict setObject:strFName forKey:strKeyFN];
[dict setObject:strLName forKey:strKeyLN];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSArray arrayWithObject:dict] options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStrng = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Your required JSON is %@",jsonStrng);

这个非常强大的代码可以实现您的目标

 NSDictionary *userDic = @{strKeyFN:strFName,strKeyLN:strLName};