2014-01-24 88 views
0

你好我嘗試使用的NSDictionary 到目前爲止創建JSON字符串,我不能讓這個JSONIOS的​​NSDictionary創建JSON

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

這裏是我的代碼:

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

現在我想創造這個JSON:

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

,但我不能找到一種方法來添加「設備」對象

+0

你做的正是你爲用戶嵌套字典同樣的事情... – Wain

回答

1
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}; 
+0

完美謝謝。只需將@「user」:jsonSignUpDictionary更改爲jsonUserDictinary –

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

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

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

}

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