Ganesh
Ganesh

Reputation: 89

Create JSON format using NSString as a key and values

I have JSON format requirement something like this.

{ 
"first_name" : "XYZ", 
"last_name" : "ABC" 
}

I have values in NSString.

NSString strFName = @"XYZ"; 
NSString strLName = @"ABC";
NSString strKeyFN = @"first_name";
NSString strKeyLN = @"last_name";

And I use NSMutableDictionary

NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
[dict setObject:strFName forKey:strKeyFN];
[dict setObject:strLName forKey:strKeyLN];

then output is

{
first_name = XYZ,
last_name = ABC
}

So I don't want "=" separating key & values instead I want ":" to separate key and values

I have went most of the stack overflow questions but didn't help getting "=" only in output

So please any help ?

Upvotes: 1

Views: 412

Answers (7)

balkaran singh
balkaran singh

Reputation: 2786

you have to convert NSMutableDictionary into NSData

then convert the NSData Into json string you want

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

Upvotes: 0

Jamil
Jamil

Reputation: 2999

this very robust code to achieve you goal

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

Upvotes: 0

iVarun
iVarun

Reputation: 6631

Here is your answer :

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

See image

Upvotes: 1

user5917312
user5917312

Reputation:

try this:-

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

Upvotes: 0

Abhinandan Pratap
Abhinandan Pratap

Reputation: 2148

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

Upvotes: 0

Himanshu padia
Himanshu padia

Reputation: 7720

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

Upvotes: 0

Chirag Sondagar
Chirag Sondagar

Reputation: 619

You write this code after your NSDictionary

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

Upvotes: 0

Related Questions