1

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 ?

1

7 Answers 7

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

@ivaran thank you but If I want to skip number from double quotes means what I have to do ?
Yes that is really useful ! Thank you very much!
0

You write this code after your NSDictionary

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

1 Comment

Yes that is really helpfull but it is adding double quotes to number too, so I don't want double quotes for it, than what I have to do ? something like this { "Number" : 7 }
0
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);

Comments

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

Comments

0

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

2 Comments

Yes that is really helpfull thank you, but it is adding double quotes to number too, so I don't want double quotes for it, than what I have to do ? something like this { "Number" : 7 }
if you are not getting see this example stackoverflow.com/questions/26817932/…
0

this very robust code to achieve you goal

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

Comments

0

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

2 Comments

Yes that is really helpfull but lets say I have number and I don't want double quotes for it, than what I can do ?
in NSDictionary key is always String But its value can be number or string . if you use value as number you not get double quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.