gpt4 book ai didi

ios nsdictionary 创建 json

转载 作者:行者123 更新时间:2023-11-28 20:08:02 25 4
gpt4 key购买 nike

你好,我正在尝试使用 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"}}

但我找不到添加“设备”对象的方法

最佳答案

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

关于ios nsdictionary 创建 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21332619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com