Parse HTML in Objective-C

Hello!
This is for the iphone

This is what i am trying to achieve:

This website expands URLs like bit.ly/miniscurl the creator got an API that says that all you have to do is go to this website:

http://expandurl.appspot.com/expand?url=YOURURL

so when i put:

http://expandurl.appspot.com/expand?url=bit.ly/miniscurl

it returns a new website with this information:

{
    "status": "OK", 
    "end_url": "http:\/\/miniscurl.dafk.net", 
    "redirects": 1, 
    "urls": ["http:\/\/bit.ly\/miniscurl", "http:\/\/miniscurl.dafk.net"], 
    "start_url": "http:\/\/bit.ly\/miniscurl"
}

And that is great! but how do i get that information into an NSString and then search through it for the different tags. so that at last got this:

NSString *status = OK;
NSString *end_url = http:\/\/miniscurl.dafk.net";
etc...

Also a NSArray containing all the redirects (if there are more than one) would have been great!

Conclusion:

I need:

  • a fast and easy way to get HTML source from a website.
  • a fast and easy way to search through NSString and cut it up in different pieces.

Thank you!

Best regards Kristian

Answers


To get the data from an HTTP-server, you can use [NSData dataWithContentsOfURL:].

To parse the data, in this case it seems to be JSON-data, you can use a simple JSON parser like TouchJSON.

Here is some example code I wrote for one of my apps:

NSURL *url = [NSURL URLWithString:@"http://server.com/data.json"];
NSData *rawJsonData = [NSData dataWithContentsOfURL:url];

CJSONDeserializer *parser = [CJSONDeserializer new];
NSError *error;
NSDictionary *jsonDictionary = [parser deserializeAsDictionary:rawJsonData 
                                                         error:&error];

[parser release];

Hope that helps!

Also, it looks like you are new here. Please mark the answer that helps you most by clicking the "√" on the left, thanks!


Need Your Help

Reordering controls on UITableView while not in editing mode?

iphone ios4 uitableview order

I have a UITableView and I want to be able to reorder the rows when I'm not in edit mode, meaning I don't want to see the delete icons until I press Edit. I want to be able to see and use the reord...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.