2016-06-21 5 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

これは私のために働いた溶液でした。次に、デバイスのキーと値のペアを追加する前に、そこに各辞書の可変コピーを作成します。

2

あなただけMutableにごNSDictionaryの、その葉を変換することによってこれを行うことができ、あなたは、受信したデータからJSONを解析し、それはあなたを与えるオプションを読み込むようNSJSONReadingMutableLeavesを使用している

まず、ここでは複数のオプションを持っています上記以外の場合

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

は、あなたがどんなNSArray、または変更可能な使用して、以下のコードにNSDictionaryオブジェクトを変換の下のようなすべての変更可能なデータ...

0あなたはMutable辞書を持っているときだけ反復し、最後に

NSMutableDictionary *dictionary=[originaldictionary deepMutableCopy]; 

のようにそれを使用して値を変更し、あなたが行われ

for(NSMutableDictionary *device in dictionary[@"protectedDevices"]){ 
    NSMutableArray *endpoints=[[NSMutableArray alloc] init]; 
    [device setObject:endpoints forKey:@"endpointsConnected"]; 
} 

の下などで問題ありません。

あなたのお役に立てば幸いです。具体

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]; 

       } 

      } 

     } 

、endpointsConnected配列内の辞書を介した反復ループのための第二:

+0

これは機能しませんでした。オリジナルディクショナリをdeepMutableCopyにキャストしようとすると例外が発生する – arcade16

+1

@ arcade16例外はありますか?これはうまく動作します、私はすべてのプロジェクトでそれを使用して、あなたはあなたが行っている変換であなたの質問を更新できますか? – iphonic

関連する問題