gpt4 book ai didi

ios - 如何为JSON字典添加值?

转载 作者:行者123 更新时间:2023-12-01 20:09:58 25 4
gpt4 key购买 nike

我有一个响应对象,看起来像这样:

[
{
"ser": "XXX-Demo-L",
"name": "XXX-Demo-L",
"statusId": 2,
"buildVersion": "XXX",
"IP": "192.168.128.123",
"priority": 1,
"bandwidthUpload": 5174.68,
"bandwidthDownload": 1554.88,
"bandwidthInternal": 37.29,
"totalBandwidthUpload": null,
"totalBandwidthDownload": null,
"totalBandwidthInternal": null,
"usage": null,
"protectedDevices": [
{
"deviceType": 3,
"securityModeId": 1,
"statusId": 2,
"uniqueId": "Samsung TV",
"deviceName": "Samsung TV",
"IP": "196.128.0.5",
"MAC": null,
"priority": 1,
"bandwidthUpload": 5184.36,
"bandwidthDownload": 1954.81,
"bandwidthInternal": 98.64,
"totalBandwidthUpload": null,
"totalBandwidthDownload": null,
"totalBandwidthInternal": null,
"usage": null,
"endpointsConnected": []
},
{
"deviceType": 3,
"securityModeId": 1,
"statusId": 2,
"uniqueId": "AARP-Demo-L-100",
"deviceName": "AARP-Demo-L-100",
"IP": "196.128.0.100",
"MAC": null,
"priority": 2,
"bandwidthUpload": 5032,
"bandwidthDownload": 1451.78,
"bandwidthInternal": 81.96,
"totalBandwidthUpload": null,
"totalBandwidthDownload": null,
"totalBandwidthInternal": null,
"usage": null,
"endpointsConnected": [
{
"endpoint": "208.91.197.104",
"ip": "208.91.197.104",
"domain": null,
"latitude": 18.4167,
"longitude": -64.6167
},
{
"endpoint": "209.85.128.2",
"ip": "209.85.128.2",
"domain": null,
"latitude": 37.4192,
"longitude": -122.0574
},
{
"endpoint": "98.137.236.24",
"ip": "98.137.236.24",
"domain": "yahoo.com",
"latitude": 37.4249,
"longitude": -122.0074
},
{
"endpoint": "204.79.197.212",
"ip": "204.79.197.212",
"domain": null,
"latitude": 47.6801,
"longitude": -122.1206
}
]
},

我正在尝试将所有端点数据添加到单个数组,但是需要设备名称与每个端点一起使用(每个设备可以有多个端点)。

这是我遍历此响应对象的代码如下所示:
NSLog(@"TELEMETRY DETAILS RESULTS ARRAY: %@", resultsArray);

self.telemetryData = resultsArray;
self.deviceListData = [resultsArray[0] valueForKey:@"protectedDevices"];

self.endpointData = [[NSMutableArray alloc] init];

for(int i = 0; i < [self.deviceListData count]; i++){

if ([self.deviceListData[i] valueForKey:@"endpointsConnected"]) {

[self.endpointData addObjectsFromArray:[self.deviceListData[i] valueForKey:@"endpointsConnected"]];

}

}

NSLog(@"Endpoint data array: %@", self.endpointData);

[self.tableView reloadData];

这是我的新数据结构的样子(我想将每个端点的设备名称添加为键/值对):
Endpoint data array: (
{
domain = "<null>";
endpoint = "208.91.197.104";
ip = "208.91.197.104";
latitude = "18.4167";
longitude = "-64.61669999999999";
},
{
domain = "<null>";
endpoint = "209.85.128.2";
ip = "209.85.128.2";
latitude = "37.4192";
longitude = "-122.0574";
},
{
domain = "yahoo.com";
endpoint = "98.137.236.24";
ip = "98.137.236.24";
latitude = "37.4249";
longitude = "-122.0074";
},

如何将设备名称添加到端点词典数组中?

最佳答案

您只能通过将NSDictionary及其叶子转换为Mutable来执行此操作,这里有多个选项,

首先,当您从接收到的数据中解析JSON时,使用NSJSONReadingMutableLeaves作为读取选项,它将为您提供所有可变数据,如下所示

NSMutableDictionary *dictionary=[NSJSONSerialization JSONObjectWithData:[NSData data] options:NSJSONReadingMutableLeaves error:nil];

如果不满足上述条件,则可以使用以下代码将任何 NSArrayNSDictionary对象转换为可变对象。
-(id)deepMutableCopy
{
if ([self isKindOfClass:[NSArray class]]) {
NSArray *oldArray = (NSArray *)self;
NSMutableArray *newArray = [NSMutableArray array];
for (id obj in oldArray) {
[newArray addObject:[obj deepMutableCopy]];
}
return newArray;
} else if ([self isKindOfClass:[NSDictionary class]]) {
NSDictionary *oldDict = (NSDictionary *)self;
NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
for (id obj in oldDict) {
[newDict setObject:[oldDict[obj] deepMutableCopy] forKey:obj];
}
return newDict;
} else if ([self isKindOfClass:[NSSet class]]) {
NSSet *oldSet = (NSSet *)self;
NSMutableSet *newSet = [NSMutableSet set];
for (id obj in oldSet) {
[newSet addObject:[obj deepMutableCopy]];
}
return newSet;
#if MAKE_MUTABLE_COPIES_OF_NONCOLLECTION_OBJECTS
} else if ([self conformsToProtocol:@protocol(NSMutableCopying)]) {
// e.g. NSString
return [self mutableCopy];
} else if ([self conformsToProtocol:@protocol(NSCopying)]) {
// e.g. NSNumber
return [self copy];
#endif
} else {
return self;
}
}

像这样使用
NSMutableDictionary *dictionary=[originaldictionary deepMutableCopy];

最后,当您有了 Mutable字典时,只需迭代并修改值即可,如下所示没有问题
for(NSMutableDictionary *device in dictionary[@"protectedDevices"]){
NSMutableArray *endpoints=[[NSMutableArray alloc] init];
[device setObject:endpoints forKey:@"endpointsConnected"];
}

您完成了。

希望对您有帮助,干杯。

关于ios - 如何为JSON字典添加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948933/

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