检查NSDictionary的数组是否包含NSDictionary

时间:2013-07-31 13:46:34

标签: ios objective-c json nsdictionary

在我的应用中,我要检查NSDictionary数组中是否还有另一个NSDictionary。换句话说,我创建了一个 JSON 文件,并将其存储在文档中。
 现在我正在解析这个JSON以插入一个新条目,在我将这个条目插入我的JSON之前,我需要检查它是否已经存储在我的文件中。我解析了JSON,所以:

// First I read the JSON file from the folder Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictContent = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
// Json parsing
NSMutableArray *array = [[NSMutableArray alloc]init];
NSMutableArray *arrayOfDictData = [[NSMutableArray  alloc]init];
if (dictContent) {
  NSDictionary *sites = [dictContent objectForKey:@"sites"];
  NSArray *site = [sites objectForKey:@"site"];
  array = [site mutableCopy];
}

然后我还有另一个NSDictionarydictForJson),其中我插入了从HTML网站解析的数据,所以现在我需要检查字典dictForJson中的数据是否是仍然在JSON文件中。
我怎么检查呢?

dictForJson有这样的结构:

dictForJson = @{@"name": htmlTitle,
                    @"src": path,
                    @"expiryDate": expireDate};

NSArray网站具有以下结构:

site: (
        {
        expiryDate = "29 Ago 2013";
        name = Sito4;
        src = "/Users/redoddity/Library/Application Support/iPhone Simulator/6.1/Applications/2D9EBE71-4365-448D-8AD2-A08749B8DBC1/Documents/Sito4.html";
    }
)

你有什么想法吗? 我尝试使用[array containsObject:dictForJson],但NSDictionary没有订购,因此当我使用此方法时,有时返回0,其他时间返回1 也许我使用以下代码构建我的JSON是有用的:

- (void)JsonCreateOrEdit {
    NSDictionary *dictSites = [[NSDictionary alloc]init];

    NSDictionary *dictSite = [[NSDictionary alloc]init];

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

    NSDictionary *dictForJson = [[NSDictionary alloc]init];

    dictForJson = @{@"name": htmlTitle,
                    @"src": path,
                    @"expiryDate": expireDate};

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"data.json"];
    BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];

    if (!fileExist) {
        [site addObject:dictForJson];
        dictSite = @{@"site": site};
        dictSites = @{@"sites": dictSite};
        // Creo il file Json
        NSLog(@"Il file non esiste");
        SBJsonWriter *writer = [[SBJsonWriter alloc]init];
        NSString *jsonCommand = [writer stringWithObject:dictSites];
        [CreateFile createFileIn:documentsPath with:@"data.json" andData:jsonCommand]; // This is a class method I made to create the file
    else {
        // Here there are the instructions to update the file data.json
         NSLog(@"Il file esiste");
    // Leggo il file json
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"];
    NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dictContent = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    // Faccio il parsing del json
    NSMutableArray *array = [[NSMutableArray alloc]init];
    NSMutableArray *arrayOfDictData = [[NSMutableArray  alloc]init];
    if (dictContent) {
        NSDictionary *sites = [dictContent objectForKey:@"sites"];
        NSArray *site = [sites objectForKey:@"site"];
        array = [site mutableCopy];
        // Here I must check if in the array site there are the same information I'm trying to insert
    }
}

2 个答案:

答案 0 :(得分:0)

NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"%K LIKE[c] %@", @"name", @"htmlTitle"];
NSArray *theFilteredArray = [mainArrayOfDictionry filteredArrayUsingPredicate:aPredicate];

if ([theFilteredArray count]){
    NSLog(@"Dictionay Exists");
}else{
     NSLog(@"Dictionay does not Exists");
}

在第一个语句中,第一个参数是" Key"第二个参数是字典的实际值。

我假设关注:

NSDictionary *dictForJson = @{@"name": htmlTitle,
                    @"src": path,
                    @"expiryDate": expireDate};


NSArray *mainArrayOfDictionry = (

"dictForJson" : @{@"name": htmlTitle,
                    @"src": path,
                    @"expiryDate": expireDate};
),
(),
(),
(),
.
.
.
)

* 希望这会对你有帮助。 *数组

答案 1 :(得分:0)

我做到了! :) 我这样做了:

{
        // Aggiorno il file
        NSLog(@"Il file esiste");
        // Leggo il file json
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"];
        NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
        NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dictContent = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        // Faccio il parsing del json
        NSMutableArray *array = [[NSMutableArray alloc]init];
        NSMutableArray *arrayOfDictData = [[NSMutableArray  alloc]init];
        if (dictContent) {
            NSDictionary *sites = [dictContent objectForKey:@"sites"];
            NSArray *site = [sites objectForKey:@"site"];
            NSDictionary *dict = [site objectAtIndex:0];
            NSLog(@"dictForJson = %@\ndict = %@", dictForJson, dict);
            NSString *name = [dict objectForKey:@"name"];
            NSLog(@"name: %@", name);
            array = [site mutableCopy];

        }

它有效!谢谢你的时间!