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:

Thank you!

Best regards Kristian

Upvotes: 0

Views: 704

Answers (1)

BastiBen
BastiBen

Reputation: 19870

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!

Upvotes: 1

Related Questions