2016-06-21 141 views
1

我有一個響應對象,看起來像這樣:如何將值添加到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"; 
    }, 

如何將設備名稱添加到我的端點字典數組中?

+0

您可以創建一個包含設備名稱的值:鍵組合的新字典。 – Kiley

回答

0

這是爲我工作溶液:

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"]) { 

       for(int x = 0; x < [[self.deviceListData[i] valueForKey:@"endpointsConnected"] count]; x++){ 

        NSMutableDictionary *m = [[self.deviceListData[i] valueForKey:@"endpointsConnected"][x] mutableCopy]; 

        if ([self.deviceListData[i] valueForKey:@"deviceName"] != (id)[NSNull null]) { 

         [m setObject:[self.deviceListData[i] valueForKey:@"deviceName"] forKey:@"deviceName"]; 

        } 

        [self.endpointData addObject:m]; 

       } 

      } 

     } 

具體地,第二for循環迭代其通過endpointsConnected陣列中的字典。然後在添加設備鍵/值對之前,在那裏製作每個字典的可變副本。

2

您只能通過轉換你的NSDictionary和葉Mutable做到這一點,你已經在這裏了多個選項,

當你從接收到的數據解析JSON,使用NSJSONReadingMutableLeaves作爲閱讀選項,它會給你一所有的可變數據像下面

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

如果不是上述原因,你轉換任何NSArray,或者NSDictionary對象使用如下代碼可變..

-(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"]; 
} 

和你做沒問題。

希望它可以幫助你,乾杯。

+0

這沒有奏效。嘗試將originalDictionary轉換爲deepMutableCopy時出現異常 – arcade16

+1

@ arcade16有什麼異常?這工作正常,我用它在每個項目中,你可以更新你正在做的轉換你的問題? – iphonic