从 nsarray 获取特定记录

get the specific record from nsarray

你好 在nsarray中数据是这样的

(
        {
        Trip =         {

            "trip_id" = 41;
            "trip_post_date" = "2016-03-28 07:52:19";

            "user_id" = 65;
        };
        User =         {

            "first_name" = irfan;
            "last_name" = sheikh;

            "user_id" = 65;
        };
        "arrival_country" =         {
            "city_name" = "Feldkirchen in Karnten";
            "country_name" = Austria;
            id = 272221;

        };
        "departure_country" =         {
            "city_name" = "Colonia La Tordilla";
            "country_name" = Argentina;
            id = 1234057;

        };
    },
        {
        Trip =         {

            "trip_id" = 40;
            "trip_post_date" = "2016-03-28 07:50:48";

            "user_id" = 65;
        };
        User =         {

            "first_name" = irfan;
            "last_name" = sheikh;


        };
        "arrival_country" =         {
            "city_name" = Karachi;
            "country_name" = Pakistan;
            id = 67008;

        };
        "departure_country" =         {
            "city_name" = Lahore;
            "country_name" = Pakistan;
            id = 112623;

        };
    }
)

我怎样才能只获得具有我提供的行程 ID 的记录。就像从代码中一样,我怎样才能得到具有 trip_id 41

的记录

假设你的 NSArray 是 mainArr.

NSString *tripID = @"41";
NSArray *tripIDArr [mainArr valueForKey :@"Trip"]valueForKey : @"trip_id"];
 NSInteger index = [tripIDArr indexOfObject :tripID];
NSDictionary *dict = [mainArr objectAtIndex:index];

试试上面的代码行。希望对你有帮助。

您打印的json对象是NSDictionaryNSArray(例如称为standardArr)。例如,您预期的字典是 desiredDict。现在你可以这样做了,

NSArray *standardArr;  //your array which you have put in your question
NSDictionary *desiredRecord;  //resulting record

for (int i = 0; i < standardArr.count; i++) {

    NSDictionary *tempDict = [standardArr objectAtIndex:i];

    NSDictionary *tempDict2 = [tempDict objectForKey:@"Trip"];

    NSString *strID = [tempDict2 valueForKey:@"trip_id"];

    if ([strID isEqualToString:@"41"]) {


        desiredRecord = tempDict; //if you want whole record

        //or

        desiredRecord = tempDict2;  //if you want only Trip record

        //or

        desiredRecord = [tempDict objectForKey:@" User"]; //if you want User record etc.....

        break;
    }

}

在 swift 中转换它,我认为这不是一项艰巨的任务..!

希望这会有所帮助:)

您需要使用 NSPredicateNSArray 中过滤您的对象。以下代码段将从嵌套数组中找到对象。

JSON

NSArray *tripAarray  = @[
 @{@"Trip":@{
             @"trip_id" : @"41",
             @"trip_post_date" : @"2016-03-28 07:52:19",
             @"user_id" : @65
     },
     @"User" :@{
             @"first_name" : @"irfan",
             @"last_name" : @"sheikh",
             @"user_id" : @65
     },
     @"arrival_country" :@{
             @"city_name" : @"Feldkirchen in Karnten",
             @"country_name" : @"Austria",
             @"id" : @272221
     },
     @"departure_country" :@{
             @"city_name" : @"Colonia La Tordilla",
             @"country_name" : @"Argentina",
             @"id" : @1234057
     }
   },
 @{@"Trip":@{
           @"trip_id" : @"42",
           @"trip_post_date" : @"2016-03-28 07:52:19",
           @"user_id" : @65
           },
   @"User" :@{
           @"first_name" : @"irfan",
           @"last_name" : @"sheikh",
           @"user_id" : @65
           },
   @"arrival_country" :@{
           @"city_name" : @"Feldkirchen in Karnten",
           @"country_name" : @"Austria",
           @"id" : @272221
           },
   @"departure_country" :@{
           @"city_name" : @"Colonia La Tordilla",
           @"country_name" : @"Argentina",
           @"id" : @1234057
           }
   }];

Objective-C

// Create a Predicate with mapping to trip_id 
NSPredicate *filterByTrip = [NSPredicate predicateWithFormat:@"%K.%K CONTAINS[c] %@", @"Trip",@"trip_id",@"41"];   

// Filter your main array with predicate, resulting array will have filtered objects 
NSArray *filteredArray = [tripAarray filteredArrayUsingPredicate:filterByTrip];

NSLog(@"%@",filteredArray);

Swift

// Create a Predicate with mapping to trip_id
var filterByTrip: NSPredicate = NSPredicate(format: "%K.%K CONTAINS[c] %@", "Trip", "trip_id", @"41")

// Filter your main array with predicate, resulting array will have filtered objects
var filteredArray: [AnyObject] = tripAarray.filteredArrayUsingPredicate(filterByTrip)
NSLog("%@", filteredArray)

当您收到 JSON 数据时,没有什么可以强制您以您需要的形式保存数据。通常这实际上是一个坏主意。大多数人会定义一个包含 userID、tripID、tripPostDate、firstName、lastName 等的模型 class,而不是返回一个字典数组,验证所有传入数据,并创建一个模型对象数组。

如果您经常需要通过其 tripID 访问一个这样的模型对象,您只需创建一个字典映射 tripID 到模型对象并在该字典中查找对象 - 一旦创建了字典,即使使用数千次旅行。