Iphone 从字符串中分离URL

Iphone 从字符串中分离URL,iphone,string,Iphone,String,如何仅从该字符串中分离url。 "":"http://flut.psites.info/api/uploads/13602519341.jpg“}” 我是这样做的: arr = [returnString componentsSeparatedByString:@"image"]; NSLog(@"********%@",arr); self.mImgUrlStr = [arr objectAtIndex:1]; NSLog(@"imgUrl: %@",self.mIm gUrlStr);

如何仅从该字符串中分离url。 "":"http://flut.psites.info/api/uploads/13602519341.jpg“}”

我是这样做的:

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

self.mImgUrlStr = [arr objectAtIndex:1];
NSLog(@"imgUrl: %@",self.mIm gUrlStr);
这是原始字符串 {“message”:“true”,“image”:http://flut.psites.info/api/uploads/13602544571.jpg“}”


这是唯一需要的url。

我想你可以用一些基本的字符串组合来解决这个问题。试试这个

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);
这将打印出:

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

希望这有帮助

我想你可以用一些基本的字符串组合来解决这个问题。试试这个

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);
这将打印出:

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

希望这有帮助

以下是解析JSON输出的更好方法:

// 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");
        }
    }
}

以下是解析JSON输出的更好方法:

// 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");
        }
    }
}

请再发一些代码…是的,回显@Aman。。。发布更多您试图分离的内容(例如更多“
returnString
”)。它看起来/闻起来像JSON。请尝试使用NSScanner。示例->。您可以扫描到http并从那里继续。或者,您可以尝试使用JSON解析器,尽管这可能有些过分。您是否可以发布任何示例,如我的问题中所述。使用JSON解析器解析到字典中,然后获取键“image”的值。请发布更多代码..是的,要回显@Aman。。。发布更多您试图分离的内容(例如更多“
returnString
”)。它看起来/闻起来像JSON。请尝试使用NSScanner。示例->。您可以扫描到http并从那里继续。或者,您可以尝试使用JSON解析器,尽管这可能有点过分。您是否可以发布它的任何示例,如我的问题中所述。使用JSON解析器解析到字典中,然后获取键“image”的值。不,不是。第一个字符串正是我需要的。啊,我明白了。在你发布代码之前,你能做一个'returnString'的NSLog,让我知道打印出来的是什么吗?上面是url,“:”这是额外的不需要的,结尾的}括号不是必需的。不,它不是。第一个字符串正是我需要的。啊,我明白了。在你发布代码之前,你能做一个'returnString'的NSLog,让我知道打印出来的是什么吗?上面是url,“:”这是额外的,不需要,}括号结尾不需要。