gpt4 book ai didi

ios - 处理 NSDictionary 数据

转载 作者:行者123 更新时间:2023-11-28 21:24:22 24 4
gpt4 key购买 nike

以下是来自网络服务的数据。我想将 key 和对组合成数组。基本上,我收到的数据是带有键值对的字典数组,但是键“记录”也是字典数组。如何处理这个?我在下面给出了我想要的输出。如果有人可以帮助我,那就太好了。请注意,“记录”可以是任何 1、2 或更多(为了说明目的,我将记录计数设为 1 和 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";
}
)

我想要像下面这样的输出

  {
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";
}
)

最佳答案

我认为这应该做你想做的,你最终会得到一个包含每个记录对象的字典的数组。 (假设 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];
}
}

关于ios - 处理 NSDictionary 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38657497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com