如何仅将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);
这是原始字符串 { “消息”: “真”, “图像”: “http://flut.psites.info/api/uploads/13602544571.jpg”}“
只需要这个网址。
答案 0 :(得分: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");
}
}
}
答案 1 :(得分: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
希望这有帮助!