如何将 Nsmutabledictionary 数据添加到 Nsmutablearray。?

How to add Nsmutabledictionary data into Nsmutablearray.?

i am geting data in a nsmutabledictionay from api and i need to add this data into tableview cell, so i need to convert it into nsmutable array. but when tried to add data from dictonary to aarray it picks only last object of dictonary. pleae look at my code and response.

i need to parse stop1 and stop2 address 

 NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *task = [session dataTaskWithRequest:theRequest
                                        completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {

                                  NSLog(@"Response:%@ %@\n", response, error);
                                  if(error == nil)
                                  {

                                      NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                      NSLog(@"Data = %@",text);
                                  }

                                  dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];
                                  NSMutableDictionary *temp = [NSMutableDictionary new];

                                  NSArray *IDArray = [dictionary objectForKey:@"docs"];
                                  for (NSDictionary *Dict in IDArray)
                                  {
                                      [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                                      NSLog(@"id %@", temp);


                                  }


                                  [getid addObject:temp];
                                  NSLog(@"getid details %@", getid);


                                  if (getid)
                                  {
                                      [_ScheduleRidetable reloadData];
                                  }



                              }];

this is my response which i get in console

   id {
"_id" = 573c1beb7051509c0ec94936;
     }
 2016-05-18 14:41:48.641 GGR Driver Staging[11546:6724511] id {
"_id" = 573c17f8261b2bc76e228721;
    }
  2016-05-18 14:41:48.642 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0f20261b2bc76e22871e;
     }
 2016-05-18 14:41:48.642 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0eb4261b2bc76e22871d;
  }
 2016-05-18 14:41:48.643 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0e15261b2bc76e22871c;
    }
   2016-05-18 14:41:48.644 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0d0c261b2bc76e22871b;
     }
  2016-05-18 14:41:48.645 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0c5f261b2bc76e22871a;
  }
  2016-05-18 14:41:48.646 GGR Driver Staging[11546:6724511] id {
"_id" = 573c0acf261b2bc76e228717;
   }
2016-05-18 14:41:48.647 GGR Driver Staging[11546:6724511] getid details (
    {
    "_id" = 573c0acf261b2bc76e228717;
     }
  )


{"message":"success","data":{"docs":[{"_id":"573c1beb7051509c0ec94936","user":{"phone":"9574863210","image":"","name":"user12"},"distance":45123,"bookingNumber":"429r2ckh8pa8","bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":103.99,"estimatedDuration":"4557","created":"2016-05-18T07:38:12.037Z","stop2":{"address":"New Delhi, New Delhi 110001, India","location":[28.6139391,77.2090212]},"stop1":{"address":"Sector 18, Noida, Uttar Pradesh 201301, India","location":[28.570317000000003,77.3218196]},"destination":{"address":"Gurgaon, Haryana 122001, India","location":[28.459496499999997,77.0266383]},"currentLocation":{"address":"A20A Block, Sector 58","location":[28.6024597,77.3609439]}},{"_id":"573c17f8261b2bc76e228721","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0f20261b2bc76e22871e","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0eb4261b2bc76e22871d","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":9913,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":31.71,"estimatedDuration":"1533","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida, Uttar Pradesh, India","location":[28.5355161,77.39102649999995]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0e15261b2bc76e22871c","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0d0c261b2bc76e22871b","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0c5f261b2bc76e22871a","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}},{"_id":"573c0acf261b2bc76e228717","user":{"phone":"9988776655","image":"","name":"Chandan"},"distance":5003,"bookingType":0,"paymentMode":"Card","tip":0,"estimatedFare":14.83,"estimatedDuration":"703","created":"2016-05-17T07:44:02.130Z","stop2":{"location":[]},"stop1":{"location":[]},"destination":{"address":"Noida City Center, Noida, Uttar Pradesh, India","location":[28.5748053,77.35633740000003]},"currentLocation":{"address":"A20, A Block, Sector 58, Noida, Uttar Pradesh 201301, India","location":[28.6024597,77.36094389999994]}}],"total":10,"limit":8,"page":":1","pages":2}}

This is my api form there i get response.

NSURL *theURL = [NSURL URLWithString:@"http://qa.networc.in:1336/api/dispatcher/rideScheduled/:1"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL      cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];

//Specify method of request(Get or Post)
[theRequest setHTTPMethod:@"GET"];

//Pass some default parameter(like content-type etc.)
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

[theRequest addValue:token forHTTPHeaderField:@"x-access-token"];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
if (responseData == nil)
{

    return;
}

NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&theError];
NSLog(@"url to send request= %@",theURL);
NSLog(@"%@",dataDictionaryResponse);
   Array *IDArray = [dictionary objectForKey:@"docs"];
             for (NSDictionary *Dict in IDArray)
            {
               NSMutableDictionary *temp = [NSMutableDictionary new];
               [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                [getid addObject:temp];

             }

因为NSMUtableDictionary只有一个key,可以

for(int i=0,i< IDArray.count,i++){
    [temp setObject:[Dict objectForKey:@"_id"] forKey:[NSString stringformationWith @"_id%d",i]];
}
Use this code,

NSMutableArray *tempArray=[[NSMutableArray alloc]init];

for (NSDictionary *Dict in IDArray)
{
    [tempArray setValue:[Dict objectForKey:@"_id"] forKey:@"id"];
};

[allValues addObjectsFromArray:tempArray];

allValues 也是 NSMutableArray,

希望对您有所帮助

喜欢

                 getid = [NSMutableArray New];

                    for (NSDictionary *Dict in IDArray)
                              {
                                   NSMutableDictionary *temp = [NSMutableDictionary new];
                                  [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                                 // for fetch stop1 and stop2 use like
                                   NSMutableDictionary *stp1 = [Dict objectForKey:@"stop1"] 
                               if ([[stp1 allKeys] containsObject:@"address"]) {
// key exists.
                         [temp setObject:[stp1 objectForKey:@"address"] forKey:@"address"];
                            }
               }


                                NSMutableDictionary *stp2 = [Dict objectForKey:@"stop2"] ;

                         if ([[stp2 allKeys] containsObject:@"address"]) {
// key exists.
                           [temp setObject:[stp2 objectForKey:@"address"] forKey:@"address1"];
                            }

                                 [getid addObject:temp];
                              }

                             if (getid.count>0)
                              {
                                  [_ScheduleRidetable reloadData];
                              }

第 2 步

在表视图中检索

 idname.text  = [getid objectAtIndex:objectAtIndex:indexPath.row]objectForKey:@"_id"];
  stop2.text  = [getid objectAtIndex:objectAtIndex:indexPath.row]objectForKey:@"address1"];
    stop1.text  = [getid objectAtIndex:objectAtIndex:indexPath.row]objectForKey:@"address"];

您在 for 循环 [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"]; 中为同一键设置值,此语句每次都会覆盖 _id 的值,因此当您将它添加到 getid 数组时,它只有最后一个 _id 及其价值。

所以现在,如果您想要字典数组,那么您只需将 [getid addObject:temp]; 语句放入 for 循环(最后)

如果你想要它的数组值(字符串数组),这里的字符串意味着你的 id,比如 573c0acf261b2bc76e228717 那么你可以做类似的事情,

在你的 for loop

  [getid addObject:[Dict objectForKey:@"_id"] forKey:@"_id"];

所以这只会在 getid 数组中添加 id。你的数组看起来像:[573c17f8261b2bc76e228721, 573c0f20261b2bc76e22871e, 573c0eb4261b2bc76e22871d, etc to last id]

希望这会有所帮助:)