JSON 的 NSMutableDictionary
Posted
技术标签:
【中文标题】JSON 的 NSMutableDictionary【英文标题】:NSMutableDictionary for JSON 【发布时间】:2012-05-23 15:12:53 【问题描述】:我遇到了一个问题!
我有一个带有 JSON 的字符串,例如:
"用户名":"用户名","密码":"密码"
我使用 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"
]
但是我该怎么做呢?有人可以帮帮我吗?
【问题讨论】:
【参考方案1】:这会给你补偿。使用你所拥有的和这个例子,你应该可以很容易地取得联系。
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
【讨论】:
以上是关于JSON 的 NSMutableDictionary的主要内容,如果未能解决你的问题,请参考以下文章