2012-05-23 36 views
1

我困住了一個問題!JSON的NSMutableDictionary

我有一個JSON字符串,如:

{ 「用戶名」: 「用戶名」, 「密碼」: 「密碼」}

我建這個JSON字符串中包含的Xcode:

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": "[email protected]", "name": "Name" }, 
    { "mail": "[email protected]", "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