Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-02-2011, 11:38 PM   #1 (permalink)
Registered Member
 
chrishannah's Avatar
 
Join Date: May 2011
Posts: 102
chrishannah is on a distinguished road
Default Getting part of an NSString

In my app i'm creating now i use goo.gl's url shortener api to shorted urls. I have it nearly working, I can send the longUrl to retrieve the short one in a NSString but it's in this format: { "kind": "urlshortener#url", "id": "http://goo.gl/something", "longUrl": "http://somethinglonggggg/" }

I just wondered if there is a way to just take the id (short url) from that.

Here's what I have so far:
Code:
NSString *longURL = urlText.text; NSData *reqData = [[NSString stringWithFormat:@"{\"longUrl\":\"%@\"}", longURL] dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:[NSURL URLWithString:@"https://www.googleapis.com/urlshortener/v1/url"]
                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                timeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:reqData];

NSError *err = [[NSError alloc] init];
NSData *retData = [NSURLConnection sendSynchronousRequest:request
                                        returningResponse:nil
                                                    error:&err];

if([err domain]) return;

NSString *retString = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];

if([retString rangeOfString:@"\"error\""].length) return;

NSLog(@" longUrl equals %@ ", longURL);
NSLog(@" retString equals %@ ", retString);

urlText.text = retString;

Last edited by chrishannah; 09-02-2011 at 11:43 PM.
chrishannah is offline   Reply With Quote
Old 09-03-2011, 02:11 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

that format is JSON, my advice is not to parse it with normal methods but using a framework that can easily do it, like SBJson
__________________
dany_dev is offline   Reply With Quote
Old 09-03-2011, 10:22 AM   #3 (permalink)
Registered Member
 
chrishannah's Avatar
 
Join Date: May 2011
Posts: 102
chrishannah is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
that format is JSON, my advice is not to parse it with normal methods but using a framework that can easily do it, like SBJson
In the end I worked it out.

I used JSONKit, which is quite easy to learn.
And just did 1 line of code:
Code:
NSString *shortUrl = [[retData objectFromJSONData] objectForKey:@"id"];
chrishannah is offline   Reply With Quote
Old 09-03-2011, 10:27 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by dany_dev View Post
that format is JSON, my advice is not to parse it with normal methods but using a framework that can easily do it, like SBJson
For something that simple, I think I'd avoid the overhead of including a JSON parser.

for the string:

Quote:
{ "kind": "urlshortener#url", "id": "http://goo.gl/something", "longUrl": "http://somethinglonggggg/" }
I'd use the NSString method rangeOfString: @"http://" to find the beginning of the URL, then search from that offset to the next quote, build an NSRange from the beginning of the "http" up to but not including the quote, and use substringWithRange to get the URL.

Something like this:

Code:
NSString* JSON_String = @"{ \"kind\": \"urlshortener#url\", \"id\": \"http://goo.gl/something\", \"longUrl\": \"http://somethinglonggggg/\" }";
NSRange urlRange;

//Find the location of the first @"http://" in the returned JSON string
urlRange = [JSON_String rangeOfString: @"http://"];

//Create a substring that discards everything before the "http"
NSString* trimmedString = 
  [JSON_String substringFromIndex: urlRange.location];

//Find the first quote after the "http"
urlRange = [trimmedString rangeOfString @"\""];

//Create the final string for the shortened URL.
NSString* theShortenedURL = 
  [trimmedString substringToIndex: urlRange.location];
The code above assumes that the shortened URL will always appear before the long URL in the JSON string. If that's not true you'd have to first search for urlshortener tag, then search from that point for the "http".

Disclaimer: The code above was thrown together in a couple of minutes and has not been compiled, much less tested. My code usually has a few syntax errors on the first pass. I'm a terrible proofreader. I can spot logic problems pretty quickly, but syntax errors are another story. The code above also doesn't include any error checking. That's left as an "exercise for the reader."
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-03-2011, 10:38 AM   #5 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

yes, maybe is reasonable in this specific case to use a "dirt way", but my advice should be considered as the "normal way".....also having only 2-3 things to parse do it manually is not the way IMHO.
__________________
dany_dev is offline   Reply With Quote
Old 09-03-2011, 12:03 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by dany_dev View Post
yes, maybe is reasonable in this specific case to use a "dirt way", but my advice should be considered as the "normal way".....also having only 2-3 things to parse do it manually is not the way IMHO.
Agreed.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-04-2011, 12:22 AM   #7 (permalink)
Registered Member
 
chrishannah's Avatar
 
Join Date: May 2011
Posts: 102
chrishannah is on a distinguished road
Default

In the end I kept the JSON parser, as i figured I might end up using multiple url shortener services and they may need it as well.

Thanks for the help!
chrishannah is offline   Reply With Quote
Reply

Bookmarks

Tags
goo.gl, json, nsdata, nsstring, url shortener

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 388
5 members and 383 guests
JackReidy, jeroenkeij, Sami Gh, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0