man_or_astroman
man_or_astroman

Reputation: 658

ios nsdictionary create json

Hello im trying to create json string using nsdictionary and so far i cant make this json

{"user":{"username":"test","email":"[email protected]", "password":"123456789","password_confirmation":"123456789"}

here is my code:

            NSDictionary *jsonUserDictinary = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [usernameText text], @"username",
                                           [passwordText text], @"password" ,
                                           [emailText text], @"email",
                                           [confirmText text], @"password_confirmation",
                                           nil];


        NSDictionary *jsonSignUpDictionary = [NSDictionary dictionaryWithObjectsAndKeys:jsonUserDictinary, @"user",nil];

        NSData *jsonSignUpData = [NSJSONSerialization dataWithJSONObject:jsonSignUpDictionary options:NSJSONWritingPrettyPrinted error: nil];
        NSString *jsonSignUp = [[NSString alloc] initWithData:jsonSignUpData encoding:NSUTF8StringEncoding];
        NSLog(@"json data string:\n%@", jsonSignUp);

Now i want to create this json:

{"user":{"username":"test","email":"[email protected]", "password":"123456789","password_confirmation":"123456789"},"device":{"token":"654C4DB3-3F68-4969-8ED2-80EA16B46EB0"}}

but i cant find a way to add the "device" object

Upvotes: 0

Views: 1956

Answers (4)

Arjun
Arjun

Reputation: 1

NSMutableString *mutableString = nil; NSString *string= @"";

@try
{
    if (mutableString == nil)
    {
        mutableString = [[NSMutableString alloc] init];
    }

    [mutableString appendFormat:@"{"];
    [mutableString appendFormat:@"\"string1\":%@"",",@""];
    [mutableString appendFormat:@"\"string2\":\"%@\"",@""];
    [mutableString appendFormat:@"}"];
    jsonString = mutableString ;
}
@catch (NSException *exception)
{

}
@finally
{
    return string;
}

Upvotes: 0

Ajay
Ajay

Reputation: 1622

NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                           @"test", @"username", @"[email protected]",
                           @"email",@"123456789",@"password",@"123456789",@"password_confirmation", nil];
    NSDictionary *dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                           @"token", @"654C4DB3-3F68-4969-8ED2-80EA16B46EB0", nil];
    NSDictionary *myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                                  dict1,@"user",dict2,@"device",nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDictionary
                                                       options:0
                                                         error:nil];
    NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",JSONString);

Upvotes: 1

codercat
codercat

Reputation: 23301

NSDictionary *deviceResult = [NSDictionary dictionaryWithDictionary:[YourDictionary valueForKey:@"device"]];

for (NSString *tokenKey in [deviceResult allKeys]) {
    NSDictionary *res = [deviceResult valueForKey:tokenKey];

 NSLog(@"Device Token : %@", [res objectForKey:@"token"]);

}

Upvotes: 1

Bruno
Bruno

Reputation: 109

NSDictionary *jsonUserDictinary = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [usernameText text], @"username",
                                   [passwordText text], @"password" ,
                                   [emailText text], @"email",
                                   [confirmText text], @"password_confirmation",
                                   nil];

NSDictionary *deviceDic = @{@"token": @"your token"};
NSDictionary *jsonSignUpDictionary = @{@"user":jsonSignUpDictionary, @"device":deviceDic};

Upvotes: 1

Related Questions