2

在我的应用程序中,我必须检查数组中是否NSDictionary还有另一个NSDictionary. 换句话说,我制作了一个JSON文件并将其存储在Documents中。
现在我正在解析这个 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];
}

然后我在另一个NSDictionary( dictForJson) 中插入了从 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
    }
}
4

2 回答 2

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};
),
(),
(),
(),
.
.
.
)

*希望这会对你有所帮助。*大批

于 2013-07-31T14:09:02.583 回答
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];

        }

它有效!谢谢你的时间!

于 2013-07-31T14:48:12.010 回答