Currently I'm working on an app that, like many, borrows its data from a 3rd party. In my case, I'm parsing the HTML from a website.
I had such parsing working from within the app itself using TFHpple, an XPath parser. However this meant that the iPhone would be doing all the work. Also, if the site that I'm scraping ever changed it's layout, my app would immediately stop working and my app would be out of action for at least 10 days!
To that end, on the recommendation of many fine XCakers, I began into coding a middle-man script. This script would be passed an identifer and would then go and parse the HTML generated by using that identifier. It would then return the wanted data in the form of a JSON string.
The script!
For the script, I decided upon using PHP. A HTML parser called "PHP Simple DOM Parser" was recommended to me before by @andyregan. It was a relatively simple matter to get the script to then dig down through the HTML and grab the bits I needed, i.e. the innertext!
$val = $_GET['id'];
// Create a DOM object from a URL
$html = file_get_html("http://website.location.com/script?id=$val");
// Find Nested tags
$es = $html->find('div div div');
$item1 = $es[1]->innertext;
$item2 = $es[2]->innertext;
...
$val = array("keyForItem1" => "$item1",
"keyForItem2" => "$item2");
$output = json_encode($val);
echo $output . "\n";
The function "json_encode" has been included in PHP so no external libraries were needed to access this function. More info on json_encode and php-json can be found here!
Once this was done, the iPhone app needed to be configured to access this new data source!
The App!
If using JSON in your iPhone app, there are a number of choices. For my app I chose "JSON-framework", mainly because it's ReadMe looked shorter!!!
JSON-framework allows you to parse AND generate JSON strings. In my case though, we'll only be reading them!
There are 3 methods you can use when using their code. I went for option 1!
"Option 1: Copy the JSON source files into your project
This is the simplest option and should always work—regardless of whether you're creating an application for the iPhone or a desktop Mac.
1. Drag the JSON folder from the distribution and drop it on the 'Classes' group in the 'Groups & Files' menu of your Xcode project.
2. Tick the 'Copy items into destination group's folder' option.
3. Use '#import "JSON.h"' in your source files.
If you're upgrading from a previous version, make sure you're deleting the old JSON group first, moving all the files to Trash."
It was as simple as that.
From there I just implemented a UITextField and a "Go!" button! The value entered in the UITextField would be taken, enteredID, and used as the identifier in my PHP script.
- (void) jsonTest
{
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://location.com/scriptName.php?id=%@",enteredID]];
NSString *jsonString = [NSString stringWithContentsOfURL:myURL];
jsonDict = [jsonString JSONValue];
[self checkSchemeID];
//NSLog(@"%@", jsonDict);
}
It was as simple as that. The jsonDict would then be accessed later to gain access to the data retrieved!
myLabel.text = (NSString *)[jsonDict objectForKey:@"keyForItem1"];
That's it! Aside from the pretty activity animations, that's it. One working PHP HTML parsing / JSON generation script and working interface!
$val = $_GET['id'];
// Create a DOM object from a URL
$html = file_get_html("http://website.location.com/script?id=$val");
// Find Nested tags
$es = $html->find('div div div');
$item1 = $es[1]->innertext;
$item2 = $es[2]->innertext;
...
$val = array("keyForItem1" => "$item1",
"keyForItem2" => "$item2");
$output = json_encode($val);
echo $output . "\n";
"Option 1: Copy the JSON source files into your project
This is the simplest option and should always work—regardless of whether you're creating an application for the iPhone or a desktop Mac.
1. Drag the JSON folder from the distribution and drop it on the 'Classes' group in the 'Groups & Files' menu of your Xcode project.
2. Tick the 'Copy items into destination group's folder' option.
3. Use '#import "JSON.h"' in your source files.
If you're upgrading from a previous version, make sure you're deleting the old JSON group first, moving all the files to Trash."
- (void) jsonTest
{
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://location.com/scriptName.php?id=%@",enteredID]];
NSString *jsonString = [NSString stringWithContentsOfURL:myURL];
jsonDict = [jsonString JSONValue];
[self checkSchemeID];
//NSLog(@"%@", jsonDict);
}