在我的应用程序中,我必须检查数组中是否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
}
}