天天看点

NSString的多个URL字符串的分割和截取技巧

今天开发高德云图,返回来的image是字符串组,不是Json,也不是字典,我的妈啊!你说的是真的!

preurl=(

        {

        "_id" = 54a358f2e4b053daeaf71fb7;

        "_preurl" = "http://img.yuntu.amap.com/54a358f2e4b053daeaf71fb7@!thumb";

        "_url" = "http://img.yuntu.amap.com/54a358f2e4b053daeaf71fb7@!orig";

    },

        {

        "_id" = 54a36713e4b053daeaf7213e;

        "_preurl" = "http://img.yuntu.amap.com/54a36713e4b053daeaf7213e@!thumb";

        "_url" = "http://img.yuntu.amap.com/54a36713e4b053daeaf7213e@!orig";

    },

        {

        "_id" = 54ab7e91e4b053daeaf7c7ea;

        "_preurl" = "http://img.yuntu.amap.com/54ab7e91e4b053daeaf7c7ea@!thumb";

        "_url" = "http://img.yuntu.amap.com/54ab7e91e4b053daeaf7c7ea@!orig";

    },

        {

        "_id" = 54ab7e97e4b053daeaf7c7ed;

        "_preurl" = "http://img.yuntu.amap.com/54ab7e97e4b053daeaf7c7ed@!thumb";

        "_url" = "http://img.yuntu.amap.com/54ab7e97e4b053daeaf7c7ed@!orig";

    },

        {

        "_id" = 54ab7e9de4b053daeaf7c7ef;

        "_preurl" = "http://img.yuntu.amap.com/54ab7e9de4b053daeaf7c7ef@!thumb";

        "_url" = "http://img.yuntu.amap.com/54ab7e9de4b053daeaf7c7ef@!orig";

    },

        {

        "_id" = 54ab7ea6e4b053daeaf7c7f1;

        "_preurl" = "http://img.yuntu.amap.com/54ab7ea6e4b053daeaf7c7f1@!thumb";

        "_url" = "http://img.yuntu.amap.com/54ab7ea6e4b053daeaf7c7f1@!orig";

    }

)  
           

我现在想要分割他们,并放到不同的数值或字典,直接上方法。【ps:我不知道有没有更好的方法,还有这个方法的性能如何,唉,要学习的太多,我还得慢慢来,虚心吧!少年!】

//从数据中分段
        NSArray *arry=[imageStr componentsSeparatedByString:@"}"];
        
        for (int i = 0;i < arry.count -1;i++)
        {
            NSString * imageStrring = arry[i];
            //去掉所以换行
            //imageStr = [imageStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            //去掉所有空格
            imageStr = [imageStr stringByReplacingOccurrencesOfString:@" " withString:@""];
            NSString * preurl = (NSString *) [[[[imageStrring componentsSeparatedByString:@"preurl\"=\""] objectAtIndex:1] componentsSeparatedByString:@"\";"] objectAtIndex:0];
            
            //NSLog(@"\n %@ \n",preurl);
            
            [self.thumbPhotosUrlArray addObject:preurl];
            
            NSString * url = (NSString *) [[[[imageStrring componentsSeparatedByString:@"_url\"=\""] objectAtIndex:1] componentsSeparatedByString:@"\";"] objectAtIndex:0];
            [self.origPhotosUrlArray addObject:url];
            
            //NSLog(@"\n %@ \n",url);
            
        }
        
           

有必要解释一下吗?

我的思路是,先以“}”,分割字符串,得到的数组,在一个一个的截取~~

nice~