iPhone Programmatically
iPhone Programmatically

Reputation: 1227

Separate URL from string

How to separate only url from this string. "":"http://flut.psites.info/api/uploads/13602519341.jpg"}"

I am doing this way:

arr = [returnString componentsSeparatedByString:@"image"];
NSLog(@"********%@",arr);

self.mImgUrlStr = [arr objectAtIndex:1];
NSLog(@"imgUrl: %@",self.mIm gUrlStr);

This is the original string {"message":"true","image":"http://flut.psites.info/api/uploads/13602544571.jpg"}"

From this only url required.

Upvotes: 0

Views: 105

Answers (2)

Michael Dautermann
Michael Dautermann

Reputation: 89509

Here's a better way to parse your JSON output:

// take string and put it into an NSData object (which NSJSONSerialization can work with)
NSData * dataFromString = [returnString dataUsingEncoding: NSUTF8StringEncoding];
if(dataFromString)
{
    NSError * error = NULL;
    NSDictionary * resultDictFromJSON = [NSJSONSerialization JSONObjectWithData: dataFromString options: 0 error: &error];
    if(resultDictFromJSON == NULL)
    {
        NSLog( @"error from JSON parsing is %@", [error localizedDescription]);
    } else {
        NSString * imageURLString = [resultDictFromJSON objectForKey: @"image"];
        if(imageURLString)
        {
            NSLog( @"here's your image URL --> %@", imageURLString);
        } else {
            NSLog( @"hmmm, I couldn't find an \"image\" in this dictionary");
        }
    }
}

Upvotes: 2

Madhu
Madhu

Reputation: 2384

Guess you could work this out with some basic string composition. Try this;

NSString *firstString = @"http://flutterr.pnf-sites.info/api/uploads/13602519341.jpg";
NSArray *strComponents = [firstString componentsSeparatedByString:@"/"];

NSString *finalString = [NSString stringWithFormat:@"%@//%@", [strComponents objectAtIndex:0], [strComponents objectAtIndex:2]];
NSLog(finalString);

This prints out:

2013-02-07 11:19:39.167 TestProject[91226:c07] http://flutterr.pnf-sites.info

Hope this helps!

Upvotes: 1

Related Questions