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.

6
  • please post some more code.. Commented Feb 7, 2013 at 5: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. Commented Feb 7, 2013 at 5:51
  • Try using NSScanner. Example here -> 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. Commented Feb 7, 2013 at 5:59
  • It seems right can you post any example of it, like above mentioned in my problem. Commented Feb 7, 2013 at 6:03
  • Parse into a dictionary with a JSON parser, then get value for key "image". Commented Feb 7, 2013 at 6:09

2 Answers 2

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");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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!

3 Comments

No, its not. firstString is what i need exactly.
Ah, I see. Could you do a NSLog of 'returnString' before the code you posted, and let me know what prints out?
Above is the url, ":" this is extra not required and } bracket at end not required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.