美文网首页iOS基础·OC语法篇iOS学习资料111
(ios)字符串与数组,字典,对象之间的转换

(ios)字符串与数组,字典,对象之间的转换

作者: 三生石畔 | 来源:发表于2016-07-06 10:09 被阅读6124次
    • app开发过程中,与服务器数据交流时,服务器常常需要我们把一些集合以字符串的方式传给服务器,下面就是一些字符串和数组,字典,对象之间的转换。
    /**
     *  判断自己是否为空
     *
     *  @return <#return value description#>
     */
    - (BOOL) isBlank
    {
        if (!self.length ||
            self == nil ||
            self == NULL ||
            (NSNull *)self == [NSNull null] ||
            [self isKindOfClass:[NSNull class]] ||
            [self isEqualToString:@"(null)"] ||
            [self isEqualToString:@"<null>"] ||
            [self isEqualToString:@"null"] ||
            [self isEqualToString:@"NULL"]
            ) {
            return YES;
        }else {
            return NO;
        }
    }
    
    /**
     *  json字符串,转成字典
     *
     *  字符串调用
     *  @return <#return value description#>
     */
    - (NSDictionary *)stringJsonToDictionary{
        if ([self isBlank]) {
            return nil;
        }
        NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData
                             
                                                            options:NSJSONReadingMutableContainers
                                                              error:err];
         if(err) {
            NSLog(@"json解析失败:%@",err);
            return nil;
        }
        return dictionary;
    }
    
    
    
    /**
     *  字典转json字符串
     *
     *  字典调用
     *  @return <#return value description#>
     */
     - (NSString *)dictionaryToJsonString{
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:nil];
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    /**
    *  数组转json字符串
    *
    *  数组调用
    *  @return <#return value description#>
    */
    - (NSString *)arrayToJsonString{
       NSError *parseError = nil;
       NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&parseError];
       return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
       
    }
    
    /**
    *  对象转换为字典
    *
    *  @param obj 需要转化的对象
    *
    *  @return 转换后的字典
    */
    + (NSDictionary*)getObjectData:(id)obj {
    
      NSMutableDictionary *dic = [NSMutableDictionary dictionary];
      unsigned int propsCount;
    
      objc_property_t *props = class_copyPropertyList([obj class], &propsCount);
    
      for(int i = 0;i < propsCount; i++) {
    
          objc_property_t prop = props[i];
          NSString *propName = [NSString stringWithUTF8String:property_getName(prop)];
          id value = [obj valueForKey:propName];
          if(value == nil) {
    
              value = [NSNull null];
          } else {
              value = [self getObjectInternal:value];
          }
          [dic setObject:value forKey:propName];
      }
    
      return dic;
    }
    
    + (id)getObjectInternal:(id)obj {
    
      if([obj isKindOfClass:[NSString class]]
         ||
         [obj isKindOfClass:[NSNumber class]]
         ||
         [obj isKindOfClass:[NSNull class]]) {
    
          return obj;
    
      }
      if([obj isKindOfClass:[NSArray class]]) {
    
          NSArray *objarr = obj;
          NSMutableArray *arr = [NSMutableArray arrayWithCapacity:objarr.count];
    
          for(int i = 0; i < objarr.count; i++) {
    
              [arr setObject:[self getObjectInternal:[objarr objectAtIndex:i]] atIndexedSubscript:i];
          }
          return arr;
      }
      if([obj isKindOfClass:[NSDictionary class]]) {
    
          NSDictionary *objdic = obj;
          NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:[objdic count]];
    
          for(NSString *key in objdic.allKeys) {
    
              [dic setObject:[self getObjectInternal:[objdic objectForKey:key]] forKey:key];
          }
          return dic;
      }
      return [self getObjectData:obj];
      
    }
    
    

    相关文章

      网友评论

        本文标题:(ios)字符串与数组,字典,对象之间的转换

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