我有一個響應對象,看起來像這樣:如何將值添加到JSON字典?
[
{
"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";
},
如何將設備名稱添加到我的端點字典數組中?
您可以創建一個包含設備名稱的值:鍵組合的新字典。 – Kiley