0

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.

iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
  • please post some more code.. – Aman Aggarwal Feb 07 '13 at 05:25
  • 2
    Yes, to echo @Aman... post more of what you're trying to separate (e.g. more of "`returnString`"). It looks / smells like JSON. – Michael Dautermann Feb 07 '13 at 05:51
  • Try using NSScanner. Example here -> http://stackoverflow.com/questions/594797/how-to-use-nsscanner. You could scan upto http and carry on from there. Alternatively, you could try a JSON parser, although that might be overkill. – esh Feb 07 '13 at 05:59
  • It seems right can you post any example of it, like above mentioned in my problem. – iPhone Programmatically Feb 07 '13 at 06:03
  • Parse into a dictionary with a JSON parser, then get value for key "image". – Carl Veazey Feb 07 '13 at 06:09
  • Read this question if you want to know how to use a JSON parser and you could have a variety of choices of parsers as well -> http://stackoverflow.com/questions/5547311/how-do-i-parse-json-with-objective-c – esh Feb 07 '13 at 06:14

2 Answers2

2

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");
        }
    }
}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

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!

Madhu
  • 2,429
  • 16
  • 31