Below is data from the web service. I would like to combine the key and pair into array. Basically the data i received is array of dictionary with key value pair, but again a key 'records' is array of dictionary. How to process this ? I had given my desired output below. It would be great if you someone can help me. Please note, 'records' can be any 1, 2 or more (for illustration purpose, i had given the record count as 1 and 2)
<__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";
}
)
I want the output like the below
{
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";
}
Tried
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];
Edited - Output
{
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";
}
)
If you see the above actual json object, there are 75 and 76 in aa, but only one value is saved in the final array as given below.
{
a = "some string";
b = "some string";
c = "some value";
d = "some value";
aa = 76;
bb = "some string"";
cc = "some string";
e = "some string";
}
)
I think this should do what you want, you will end up with an array containing a dictionary for every record object. (Assuming that inputArray
is an NSArray
in the format of your question.) I've tried to comment what is happening on each line.
//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];
}
}