objective-c - NSMutableDictionary 未获得预期输出。

标签 objective-c ios json nsdictionary nsmutabledictionary

它只为 NSMutableDictionary 输出一组,而不是两者。我想使用 NSMutableDictionary (JSONRepresentation) 创建 JSON 请求。

// My code    

NSArray *keysEndpoint = [NSArray arrayWithObjects:@"ID", @"Name", @"EndpointType", nil];
NSArray *objectEndpoint = [NSArray arrayWithObjects:@"622", @"Brand", @"0", nil];

NSArray *keysEndpoint1 = [NSArray arrayWithObjects:@"ID", @"Name", @"EndpointType", nil];
NSArray *objectEndpoint1 = [NSArray arrayWithObjects:@"595", @"CK-05052011", @"1", nil];

NSMutableArray *keys1 = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *objects1 =  [[NSMutableArray alloc] initWithCapacity:0];

[keys1 addObjectsFromArray:keysEndpoint];
[keys1 addObjectsFromArray:keysEndpoint1];

NSLog(@"Key Dic: %@", keys1);

[objects1 addObjectsFromArray:objectEndpoint];
[objects1 addObjectsFromArray:objectEndpoint1];

NSLog(@"Obje Dic: %@", objects1);

NSMutableDictionary *testMut = [NSMutableDictionary dictionaryWithObjects:objects1  forKeys:keys1];

NSLog(@"Test Dic: %@", testMut);

我得到的输出是这样的:

 Test Dic: {
    EndpointType = 1;
    ID = 595;
    Name = "CK-05052011";
}

我想要的扩展输出是:

Test Dic: {
    EndpointType = 1;
    ID = 595;
    Name = "CK-05052011";
} 
{
    EndpointType = 0;
    ID = 622;
    Name = "Brand";
}

最佳答案

对于字典,添加相同的键两次将覆盖第一组键。你应该有一个 NSMutableArray 的 NSMutableDictionary

NSArray *keysEndpoint = [NSArray arrayWithObjects:@"ID", @"Name", @"EndpointType", nil];
NSArray *objectEndpoint = [NSArray arrayWithObjects:@"622", @"Brand", @"0", nil];

NSArray *keysEndpoint1 = [NSArray arrayWithObjects:@"ID", @"Name", @"EndpointType", nil];
NSArray *objectEndpoint1 = [NSArray arrayWithObjects:@"595", @"CK-05052011", @"1", nil];

NSMutableDictionary *testMut = [NSMutableDictionary dictionaryWithObjects:objectsEndpoint  forKeys:keysEndpoint];


NSMutableDictionary *testMut1 = [NSMutableDictionary dictionaryWithObjects:objectsEndpoint1  forKeys:keysEndpoint1];

NSMutableArray * dictArray = [NSMutableArray arrayWithObjects:testMut,testMut1,nil];

NSLog(@"Test DictArray: %@", dictArray);

关于objective-c - NSMutableDictionary 未获得预期输出。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8419430/

相关文章:

iphone - 有人可以解释下面的符号吗?

java - OkHttp 没有得到整个 JSON

java - 在 Java EE 中使用 Ajax 传递 JSON 字符串

ios - UIScreen 边界小于外部屏幕上的预期

ios - 如何根据其在textview中的内容更改自定义tableview单元格高度和textview高度

ios - TabBar 和 NavigationBar View 部分隐藏

ios - 从 UISlider 继承时会出现一个额外的 slider

ios - 静态库,访问 self.view

iphone - IOS 下视频无法播放

javascript - JSON.解析 : unexpected character at line 1 column 2 of the JSON data (HTML)