0

如何仅从该字符串中分离 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。

4

2 回答 2

2

这是解析 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");
        }
    }
}
于 2013-02-07T06:14:07.700 回答
1

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

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

希望这可以帮助!

于 2013-02-07T05:51:27.590 回答