2016-07-29 62 views
0

以下是来自Web服务的数据。我想将键和对组合成一个数组。基本上我收到的数据是带有键值对的字典数组,但是键“记录”又是字典数组。如何处理?下面给出了我期望的结果。如果你能帮助我,这将是非常棒的。请注意,“记录”可以是任何1,2以上(图示的目的,我给了记录数为1和2)处理NSDictionary数据

<__NSCFArray 0x7feb44c0e400>(
{ 
    a = "string"; 
    b = "string"; 
    records =  (
       { 
      aa = "first array's first record"; 
      bb = "first array's first record"; 
      cc = "first array's first record"; 
     } 
    ); 
    c = "somevalue"; 
    d = "some value"; 
    e = "some value"; 
}, 


{ 
    a = "string"; 
    b = "string"; 
    records =  (
       { 
      aa = "second array's first record"; 
      bb = "second array's first record"; 
      cc = "second array's first record"; 
     }, 
       { 
      aa = "second array's second record"; 
      bb = "second array's second record"; 
      cc = "second array's second record"; 
     } 
    ); 
    c = "some value"; 
    d = "some value"; 
    e = "some value"; 
} 
) 

我要像下面

{ 
a = "string"; 
b = "string"; 
aa = "first array's first record"; 
bb = "first array's first record"; 
cc = "first array's first record"; 
c = "somevalue"; 
d = "some value"; 
e = "some value"; 
}, 

{ 
a = "string"; 
b = "string"; 
aa = "second array's first record"; 
bb = "second array's first record"; 
cc = "second array's first record"; 
c = "some value"; 
d = "some value"; 
e = "some value"; 
}, 

{ 
a = "string"; 
b = "string"; 
aa = "second array's second record"; 
bb = "second array's second record"; 
cc = "second array's second record"; 
c = "some value"; 
d = "some value"; 
e = "some value"; 
} 
输出

试过

NSDictionary *receivedDictionary = self.responseDictionary; 
    NSArray *finalArray = [[NSArray alloc]init]; 
    NSMutableArray *array; 
    for (int i=0; i<[receivedDictionary count]; i++) { 
    array = [[receivedDictionary valueForKey:@"records"]objectAtIndex:i]; 

    } 
    finalArray = [array arrayByAddingObjectsFromArray:array]; 

编辑 - 输出

{ 
     a = "some string"; 
     b = "some string"; 
     records =   (
         { 
       aa = 75; 
       bb = "some string"; 
       cc = "some string "; 
      }, 
         { 
       aa = 76; 
       bb = "some string"; 
       cc = "some string"; 
      } 
     ); 
     c = "some value"; 
     d = "some value"; 
     e = "some value"; 
    } 
) 

如果您看到上述实际的json对象,则aa中有75和76个,但只有一个值保存在最终数组中,如下所示。

{ 
     a = "some string"; 
     b = "some string"; 
     c = "some value"; 
     d = "some value"; 
     aa = 76; 
     bb = "some string""; 
     cc = "some string"; 
     e = "some string"; 
    } 
) 
+0

向我们展示您到目前为止尝试过的方法。 –

+0

我能够获取'记录'的数据,但不知道如何将所有密钥组合到数组中。请检查我编辑的版本 – user3310076

回答

2

我想这应该做你想要什么,你最终将一个数组包含每个记录对象的字典。 (假设inputArray是你问题格式的NSArray)。我试着评论每一行上发生了什么。

//First we initialize an empty array to store all the values. 
NSMutableArray *finalOutput = [NSMutableArray array]; 
//Next we loop through every dictionary in your input array. 
for (NSDictionary *outerDict in inputArray) { 
    //Make a copy of that dictionary 
    NSMutableDictionary *outerCopy = [outerDict mutableCopy]; 
    //but remove the records object as we'll add that back again in the next step 
    [outerCopy removeObjectForKey:@"records"]; 
    //Now we loop through all the records for outerDict 
    for (NSDictionary *record in outerDict[@"records"]) { 
     //Make a mutable copy 
     NSMutableDictionary *flattenedDict = [record mutableCopy]; 
     //Add back the values from the outerCopy 
     [flattenedDict addEntriesFromDictionary:outerCopy]; 
     //And add this final flattened dictionary to the output array 
     [finalOutput addObject:flattenedDict]; 
    } 
} 
+0

您能否根据我所需的输出并根据所提供的数据对此进行相应的阐述? – user3310076

+0

我已经添加了示例输出,它与您想要的输出相同,因为您传递的数组格式与您的问题中的格式相同。 –

+0

是的,你提供的OP是我想要的,但是你能详细地提供代码吗?我需要在哪里设置'a','b','c'...'aa',bb'等的值?你可以请详细的代码,因为我不能按照你提供的代码段? – user3310076

0

试试这个,

NSMutableArray *finalArray = [[NSMutableArray alloc]init]; 
    NSArray *jsonArray; // Your Json array Object 
    for(NSDictionary * outerDictionary in jsonArray) 
    { 
     NSMutableDictionary *objectDictionary = [[NSMutableDictionary alloc]init]; 
     [objectDictionary setValue:outerDictionary[@"a"] forKey:@"a"]; 

     [objectDictionary setValue:outerDictionary[@"b"] forKey:@"b"]; 



NSArray *records = outerDictionary[@"record"]; 
    for (NSDictionary *record in records) { 

      [objectDictionary setValue:record[@"aa"] forKey:@"aa"]; 

      [objectDictionary setValue:record[@"bb"] forKey:@"bb"]; 

      [objectDictionary setValue:record[@"cc"] forKey:@"cc"]; 

     } 
     [objectDictionary setValue:outerDictionary[@"c"] forKey:@"c"]; 

     [objectDictionary setValue:outerDictionary[@"d"] forKey:@"c"]; 

     [objectDictionary setValue:outerDictionary[@"e"] forKey:@"e"]; 
     [finalArray addObject:objectDictionary]; 
    } 
+0

谢谢!这是行得通的,除了一个问题,当有2条记录属于同一个字典时,则只保存一条。看我编辑的部分。有些时候有两个不同的最顶级的字典包含一条记录,那么该数组有3次,它应该只有2. – user3310076

+0

谢谢阿米特。这对一些情况也会有帮助。谢谢你的协助 ! – user3310076

0

你将不得不通过每个字典为:

for (int i=0; i<[records count]; i++) { 
     NSDictionary *dict = [records objectAtIndex:i]; 
     [dictValues addObject:[dict valueForKey:@"aa"]]; 
     [dictValues addObject:[dict valueForKey:@"bb"]]; 
     [dictValues addObject:[dict valueForKey:@"cc"]]; 

    }