如何解析数据,如果它是Objective c中的`isKindOfClass([NSString] class)`

Posted

技术标签:

【中文标题】如何解析数据,如果它是Objective c中的`isKindOfClass([NSString] class)`【英文标题】:How to parse data, if it `isKindOfClass([NSString] class)` in Objective c 【发布时间】:2017-10-06 07:01:00 【问题描述】:

我被困在这里,我是 iOS 新手,请帮助我。

现在我的dict 中有数据,但类格式不同。如果它是NSDictionary 格式,那么它运行良好,它会解析所有数据并正确显示。

现在我的 JSON 有时会返回 NSDictionary,有时它会返回 NSString。

下面是代码,读取 JSON 数据是 KindOfClass:NSDictionary

@interface demoActivityDetails()

@property (strong ,nonatomic) NSArray *activityDescription;

@end

@implementation demoActivityDetails

    - (instancetype)initWithDict:(NSDictionary *)dict
        self = [super init];

    if (self) 


     if ([dict isKindOfClass:[NSString class]]) 
                NSLog(@"String");
            //I'm stuck over here, don't know how do I parse data if it is NSString class
      
     else if ([dict isKindOfClass:[NSDictionary class]]) 
                if ([[dict valueForKey:@"demoActivityTypeCode"] valueForKey:@"value"]) 
                    self.demoActivityTypeCode = [[dict valueForKey:@"demoActivityTypeCode"] valueForKey:@"value"];
                


                if ([[[dict valueForKey:@"cityOfActivity"] valueForKey:@"cityCode"] valueForKey:@"value"]) 
                    self.cityOfOrigin = [[[dict valueForKey:@"cityOfActivity"] valueForKey:@"cityCode"] valueForKey:@"value"];
                

                if ([[[dict valueForKey:@"cityOfActivity"] valueForKey:@"cityySubdivisionCode"] valueForKey:@"value"]) 
                    self.citySubdivisionCodeArray = [[[dict valueForKey:@"cityOfActivity"] valueForKey:@"citySubdivisionCode"] valueForKey:@"value"];
                




                if ([dict valueForKey:@"activityDescription"]) 
                    self.activityDescription = [dict  valueForKey:@"activityDescription"];
                

                if ([self.activityDescription isKindOfClass:[NSArray class]]) 

                    if ([self.activityDescription valueForKey:@"languageCode"]) 
                        self.flagsArray = [self.activityDescription valueForKey:@"languageCode"];
                    

                    if ([self.activityDescription valueForKey:@"value"]) 
                        self.subTitleArray = [self.activityDescription valueForKey:@"value"];
                    

                    if (self.flagsArray && self.subTitleArray) 

                        self.subTitleWithLangCode  = [[NSMutableDictionary alloc] initWithObjects:self.subTitleArray forKeys:self.flagsArray];
                    

                    if ([self.flagsArray containsObject:@"en"]) 
                        for (int i=0 ; i < self.flagsArray.count; i++ ) 
                            if ([self.flagsArray[i] isEqualToString:@"en"]) 
                                self.subTitle = [self.subTitleArray objectAtIndex:i];
                                self.selectedFlag = [self.flagsArray objectAtIndex:i];
                            
                        
                    else 
                        self.subTitle = [self.subTitleArray firstObject];
                        self.selectedFlag = [self.flagsArray firstObject];
                    

                
                else if ([self.activityDescription isKindOfClass:[NSString class]]) 
                    self.subTitle = (NSString *)self.activityDescription;
                

            

      

        
        return self;
    

    @end

请有人帮助我。

【问题讨论】:

当你在 NSString 中接收数据时,是不是采用了这样的格式,比如@"\"demoActivityTypeCode\":\"value\":1,\"cityOfActivity\":\ "cityCode:201\",\"ContractTemplateID\":\"Content\":65,\"type\":\"text\"" 不,类似于json格式。 activityDescription = languageCode = en; value = "活动描述_en"; ; cityOfActivity = cityCode = value = 012; ; citySubdivisionCode = value = "CA"; ; ; demoActivityTypeCode = value = “SampleText”; ; 类似这个 【参考方案1】:

如果字符串是JSON 格式,您可以将其转换为NSData,然后使用NSJSONSerialization 将数据转换为JSON 对象(在您的情况下为NSDictionary)。我认为您应该根据参数引入不同的初始化程序。此外,您应该清理initWithDict 中的解析逻辑。您将因在分配给参考文献之前检查类型而获得奖励。如果后端或其他地方的数据与您的解析代码不匹配,您可以优雅地解决问题,否则可能会导致崩溃。 我会像这样重写这个类:

@interface DemoActivityDetails()

@property (strong ,nonatomic) NSArray *activityDescription;
@property (strong ,nonatomic) NSString *demoActivityTypeCode;
@property (strong ,nonatomic) NSString *cityOfOrigin;
@property (strong ,nonatomic) NSArray<NSString*> *citySubdivisionCodeArray;

@end

@implementation DemoActivityDetails

- (instancetype _Nullable)initWithObject:(id _Nonnull)object 
    if(object == nil) 
        return nil;
    
    if([object isKindOfClass:[NSDictionary class]])
        return [self initWithDict:object];
    else if([object isKindOfClass:[NSString class]])
        return [self initWithString:object];
    else
        return nil;


- (instancetype _Nullable)initWithString:(NSString* _Nonnull)string 
    if(![string isKindOfClass:[NSString class]]) 
        return nil;
    
    NSData* data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if(!data)
        return nil;
    
    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    if(json == nil || ![json isKindOfClass:[NSDictionary class]]) 
        return nil;
    
    return [self initWithDict:json];


- (instancetype _Nullable)initWithDict:(NSDictionary* _Nonnull)dict 
    if(![dict isKindOfClass:[NSDictionary class]]) 
        return nil;
    
    self = [super init];
    if(self) 
        NSDictionary* demoActivityTypeCodeDict = [dict objectForKey:@"demoActivityTypeCode"];
        if([demoActivityTypeCodeDict isKindOfClass:[NSDictionary class]]) 
            NSString* demoActivityTypeCode = [demoActivityTypeCodeDict objectForKey:@"value"];
            if([demoActivityTypeCode isKindOfClass:[NSString class]])
                self.demoActivityTypeCode = demoActivityTypeCode;
        

        NSDictionary* cityOfActivityDict = [dict objectForKey:@"cityOfActivity"];
        if([cityOfActivityDict isKindOfClass:[NSDictionary class]]) 
            NSDictionary* cityCodeDict = [cityOfActivityDict objectForKey:@"cityCode"];
            if([cityCodeDict isKindOfClass:[NSDictionary class]]) 
                NSString* cityOrigin = [cityCodeDict objectForKey:@"value"];
                if([cityOrigin isKindOfClass:[NSString class]])
                    self.cityOfOrigin = cityOrigin;
            
        

        // you can continue writing code
        // ....

    
    return self;

PS:不使用驼峰式命名类也是一种规范,而是以大写开头的类名

【讨论】:

谢谢,我试试看。

以上是关于如何解析数据,如果它是Objective c中的`isKindOfClass([NSString] class)`的主要内容,如果未能解决你的问题,请参考以下文章

如何在Objective C中使用解析版本1.12.0加载PFQueryTableViewController中的对象?

如何在 Objective C 中解析***的 JSON 数据?

Objective C 如何解析这个 NSString

如何在 Objective C 中解析 JSON 格式

如何解析 JSON 响应并在 Objective C 中使用它?

Objective C TableView清除Json解析