美文网首页
10-解析完的数据如何截串

10-解析完的数据如何截串

作者: 小胖子2号 | 来源:发表于2017-03-11 16:39 被阅读18次
    //4.解析数据
       NSString*res = [[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding];
      [SVProgressHUDshowErrorWithStatus:res maskType:SVProgressHUDMaskTypeBlack];
       NSLog(@"%@",res);
    

    **问题:解析完后出现如下情况,如何让其只显示“用户名不存在”,故用到截串,如下分析
    **


    1.png

    解决:

    2.png 3.png

    这个方法可以通用,其实就是截取这个中间的字符串

      //4.解析数据
    NSString *res = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
     //为什么要+3,因为location是第一个双引号的位置,所以+3后,正好是用户名的“用”字
     NSUInteger loc = [res rangeOfString:@"\":\""].location + 3;
    //为什么要-loc,因为要获取截取多少字符,这里的location是最后一个双引号的位置,所以-loc后,正好是要截取的字符串“用户名不存在”的长度
     NSUInteger len = [res rangeOfString:@"\"}"].location - loc;
    
    //截取字符串
     NSString *mag = [res substringWithRange:NSMakeRange(loc, len)];
    
     if ([res containsString:@"success"]) {
    
          [SVProgressHUD showSuccessWithStatus:mag maskType:SVProgressHUDMaskTypeBlack];
           }else {
    
          [SVProgressHUD showErrorWithStatus:mag maskType:SVProgressHUDMaskTypeBlack];
           }
          NSLog(@"%@---%@",res,[NSThread currentThread]);
    

    相关文章

      网友评论

          本文标题:10-解析完的数据如何截串

          本文链接:https://www.haomeiwen.com/subject/ietzgttx.html