ios nsdictionary 创建 json
Posted
技术标签:
【中文标题】ios nsdictionary 创建 json【英文标题】:ios nsdictionary create json 【发布时间】:2014-01-24 12:22:26 【问题描述】:您好,我正在尝试使用 nsdictionary 创建 json 字符串 到目前为止我无法制作这个 json
"user":"username":"test","email":"test@gmail.com", "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":"test@gmail.com", "password":"123456789","password_confirmation":"123456789","device":"token":"654C4DB3-3F68-4969-8ED2-80EA16B46EB0"
但我找不到添加“设备”对象的方法
【问题讨论】:
你所做的和你对用户嵌套字典所做的完全一样... 【参考方案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;
【讨论】:
非常感谢。只需将 @"user": jsonSignUpDictionary 更改为 jsonUserDictionary【参考方案2】:NSDictionary *deviceResult = [NSDictionary dictionaryWithDictionary:[YourDictionary valueForKey:@"device"]];
for (NSString *tokenKey in [deviceResult allKeys])
NSDictionary *res = [deviceResult valueForKey:tokenKey];
NSLog(@"Device Token : %@", [res objectForKey:@"token"]);
【讨论】:
【参考方案3】:NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"test", @"username", @"test@gmail.com",
@"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);
【讨论】:
【参考方案4】: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;
【讨论】:
以上是关于ios nsdictionary 创建 json的主要内容,如果未能解决你的问题,请参考以下文章
如何为 iOS 项目创建类引用,如 NSDictionary 类引用 - Apple Developer
iOS 开发,NSDictionary 和 Model 哪个好