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

时间:2016-05-18 09:18:37

标签: ios objective-c nsmutablearray nsmutabledictionary

  

我在api的nsmutabledictionay中提供数据,我需要将这些数据添加到tableview单元格中,所以我需要将其转换为nsmutable数组。但是当试图将数据从dictonary添加到aarray时,它只选择了dictonary的最后一个对象。请看看我的代码和回复。

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



                              }];
  

这是我在控制台中得到的回复

   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}}
  

这是我的api表格,我得到回复。

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

5 个答案:

答案 0 :(得分:1)

喜欢

                 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

在tableview中检索

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

答案 1 :(得分:1)

您正在为for循环[temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];设置相同键的值每次覆盖_id的值时,所以当您将其添加到getid数组时,它只有{{1}用它的价值。

现在,如果你想要字典数组,那么你可以简单地将你的_id语句放入for循环中(最后)

如果你想要它的数组值(字符串数组),这里的字符串意味着你的id就像[getid addObject:temp];那么你可以做类似的事情,

573c0acf261b2bc76e228717

for loop

所以这只会在getid数组中添加id。你的数组看起来像: [getid addObject:[Dict objectForKey:@"_id"] forKey:@"_id"];

希望这会有所帮助:)

答案 2 :(得分:0)

   Array *IDArray = [dictionary objectForKey:@"docs"];
             for (NSDictionary *Dict in IDArray)
            {
               NSMutableDictionary *temp = [NSMutableDictionary new];
               [temp setObject:[Dict objectForKey:@"_id"] forKey:@"_id"];
                [getid addObject:temp];

             }

答案 3 :(得分:0)

因为NSMUtableDictionary只有一个键,你可以

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

答案 4 :(得分:0)

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,

希望它有用