gpt4 book ai didi

ios - JSON 的 NSMutableDictionary

转载 作者:行者123 更新时间:2023-11-29 13:35:20 24 4
gpt4 key购买 nike

我遇到了一个问题!

我有一个带有 JSON 的字符串,例如:

{"UserName":"username","PassWord":"password"}

我用 xcode 构建这个 JSON 字符串,如下所示:

 NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:self.usernameField.text forKey:@"UserName"];
[jsonDict setValue:self.passwordField.text forKey:@"PassWord"];
NSLog(@"Dict: %@",jsonDict);

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString);

NSString *postStr = [NSString stringWithFormat:@"%@",jsonString];

但现在我想要一个带有 JSON 的字符串,例如:

{ "comp": [
{ "id": "1" },
{ "id": "2" },
{ "id": "3" }
],
"contact": [
{ "mail": "some@email.com", "name": "Name" },
{ "mail": "email@email.com", "name": "Name" }
]
}

但是我该怎么做呢?有人可以帮帮我吗?

最佳答案

这会给你补偿。使用您所拥有的和这个示例,您应该能够轻松取得联系。

NSDictionary *id1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"1", "id", nil];
NSDictionary *id2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"2", "id", nil];
NSDictionary *id3 = [NSDictionary dictionaryWithObjectsAndKeys:
@"3", "id", nil];

NSArray *ids = [NSArray arrayWithObjects:id1, id2, id3, nil];

NSDictionary *comp = [NSDictionary dictionaryWithObjectsAndKeys:ids, @"comp", nil];

此外,请查看以下库以轻松进行序列化/反序列化。 JSONKit

关于ios - JSON 的 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10722857/

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