I am writing an application that read its data in JSON format. It is supposed to be a classified ads app. For example it fetches the list of categories, the sub-categories and each ad from separate JSON files. Now i want to add a search option. The problem is that i do not know if it is possible to search through the JSON files and if it is efficient. Does anyone have a better way of doing this?
-
Have a look here http://stackoverflow.com/questions/777455/is-there-a-query-language-for-json – PeterMmm Oct 22 '13 at 11:25
-
You can load documents into NSArray and then filterUsingPredicate : [NSPredicate predicateWithBlock^: ... ] – kgu87 Oct 22 '13 at 11:28
-
@kgu87 yeah of that is what i thought first. But lets say im in the homepage and i wanna search for a 2008 BMW. So the application has to start parsing every categories details into arrays or dictionaries and then do the search. That definitely will result in a memory shortage! – Rashid Oct 22 '13 at 11:32
-
It sounds like you're trying to build a database of the information in the ads. In that case, and without access to the online ad database, you would have to download all the JSON files and build a database (sqlite or Core Data or whatever) based on that information, which would then be stored on the device. This doesn't sound like a good idea to me. – Guy Kogus Oct 22 '13 at 11:41
-
1You don't necessarily have to load contents of the files up front. You could get the file names and then examine them one by one inside the predicate. – kgu87 Oct 22 '13 at 11:47
4 Answers
If you're trying to avoid using a database, you only have a couple options. Load them into memory (up front or lazily). That has the benefit of making it much faster at the cost of memory footprint (space vs time). But in your comments, you said that uses to much memory. The other option is to load and unload every JSON document every time. But if you were worried about the memory footprint due to the number of documents, this will likely be too slow. There's a variant where you could create your own homegrown index but at that point, you're starting to create your own database.
So, in the end, it sounds like you're working to hard trying to avoid using a database.
Consider CoreData or SqlLite.
If you want to work with simple JSON documents, also consider EJDB which is an embedded JSON document based document db (like MongoDb). There is also an objective-c wrapper for EJDB known as EJDBKit.
It aims to be a fast MongoDB-like library which can be embedded into C/C++, .Net, NodeJS, Python, Lua, Go, Java and Ruby applications under terms of LGPL license.
EJDB is the C library based on modified version of Tokyo Cabinet.
JSON representation of queries and data implemented with API based on C BSON

- 38,941
- 11
- 91
- 99
To search from the JSON
file first you need to parse it.
Follow this steps:
NSData *theData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fileName"ofType:@"json"]];
NSDictionary *myJSONData = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];
Once you get the NSDictionary
then you can use NSPredicate
to search through it.
Hope this will help you

- 2,253
- 4
- 17
- 29
-
-
Ok i know it needs to be parsed. But im writing a classified ads app. When youre in the homepage and u wanna search for a particular item, all the JSON objects have to be parsed into arrays or dictionaries. Thats not good! I need another solution. Im starting to wonder if its even possible with JSON. – Rashid Oct 22 '13 at 11:38
Take a look at these two links below for parsing from Json file in iOS:
http://www.appcoda.com/fetch-parse-json-ios-programming-tutorial/

- 2,505
- 3
- 18
- 24
Assuming you are storing the ad data locally, then another option is to use SimpleDB, a key/value data store that I wrote. With it, you would store your JSON values and then return the keys that match your requirements. (https://github.com/AaronBratcher/SimpleDB)
[SimpleDB setValue:adJSONValue forKey:myKey inTable: kAdsTable];
Your passingTest code block would parse only 1 item at a time, so memory wouldn't be an issue, which seems to be your big concern.
Your code would look something like this:
self.ads = [SimpleDB keysInTable:kAdsTable orderByJSONValueForKey:@"name" passingTest:^BOOL(NSString *key, NSString *value, NSDate *dateAdded, NSDate *dateModified) {
Ad *ad = [[Ad alloc] initWithJSON:value];
BOOL include = NO;
// do testing here
return include;
}
}];
You would then use the keys to get your rowcount for the TableView and when populating your cell, create objects as required:
UITableViewCell *cell = AdCell;
Ad *ad = [[Ad alloc] initWithJson:[SimpleDB valueForKey:[self.ads[indexPath.row]] inTable:kAdsTable];
cell.ad = ad;
return cell;
// This is assuming you have a subclass of UITableViewCell to handle the population of the cell's contents.

- 6,051
- 2
- 39
- 70