2015-06-16 31 views
3

我是新近到iPhone的,基本上在我的應用程序中,我正在解析JSON模型下面,需要將其保存在我的自定義實體類中。在iOS中使用數組獲取JSON解析錯誤

JSON模式:

{ 
    "Products": [ 
     { 
      "Pcs": [ 
       { 
        "product_id": 2, 
        "product_name": "MyProduct", 
        "category": { 
         "Clamshells": [ 
          { 
           "product_category_id": 11, 
           "product_category_name": "MyProductCategory", 
           "Sub_category": [ 
            { 
             "sub_category_id": 21, 
             "sub_category_name": "MyProductSubCategory" 
            } 
           ] 
          } 
         ], 
         "Detachables": [ 
          { 
           "product_category_id": 12, 
           "product_category_name": "MyProductCatrgory1", 
           "Sub_category": [ 
            { 
             "sub_category_id": 31, 
             "sub_category_name": "MyProductSubCategory1" 
            } 
           ] 
          } 
         ], 
         "Convertibles": [ 
          { 
           "product_category_id": 13, 
           "product_category_name": "MyProductCatrgory2", 
           "Sub_category": [ 
            { 
             "sub_category_id": 41, 
             "sub_category_name": "MyProductSubCategory2" 
            } 
           ] 
          } 
         ] 
        } 
       } 
      ] 
     } 
    ] 
} 

我已經創建了三個實體類,如:

PCs.h:

#import <Foundation/Foundation.h> 
#import "MyProductCategory.h" 

@interface PCs : NSObject 

@property (nonatomic, retain) NSString *productTitle; 
@property (nonatomic, retain) NSString *productId; 
@property (nonatomic, retain) MyProductCategory *myProductCategory; 

@end 

MyProductCategory.h:

#import <Foundation/Foundation.h> 
#import "Clamshell.h" 

@interface MyProductCategory : NSObject 

@property (nonatomic, retain) Clamshell *clamshell; 
@property (nonatomic, retain) NSMutableArray *arrayClamshells; 
@property (nonatomic, retain) NSMutableArray *arrayConvertibles; 
@property (nonatomic, retain) NSMutableArray *arrayDetachables; 

@end 

翻蓋。小時:

#import <Foundation/Foundation.h> 

@interface Clamshell : NSObject<NSCoding> 

@property (nonatomic, retain) NSString *subProductTitle; 
@property (nonatomic, retain) NSString *subProductId; 

@end 

這裏是我的分析數據代碼:

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
                    options:kNilOptions 
                     error:&error]; 


       productsDict = [json objectForKey:@"Products"]; 

       pcsArray = [[NSMutableArray alloc] init]; 

    dispatch_async (dispatch_get_main_queue(), 
          ^{ 
           for (NSDictionary *items in productsDict) 
           { 
            NSDictionary *pcsDict = [items objectForKey:@"Pcs"]; 

            for (NSDictionary *item1 in pcsDict) { 

             PCs *pcs = [[PCs alloc] init]; 
             pcs.productId = [item1 objectForKey:@"product_id"]; 
             pcs.productTitle = [item1 objectForKey:@"product_name"]; 

             //for the category            
              pcs.myProductCategory = [[MyProductCategory alloc] init]; 
              pcs.myProductCategory = [item1 objectForKey:@"category"]; 

              NSMutableArray *clamshellDict = [pcs.myProductCategory valueForKey:@"Clamshells"]; 


              NSMutableArray *array = [[NSMutableArray alloc]init]; 

              for (NSDictionary *items2 in clamshellDict) { 
               Clamshell *clam = [[Clamshell alloc] init]; 
               clam.subProductId = [items2 objectForKey:@"sub_category_id"]; 
               clam.subProductTitle = [items2 objectForKey:@"sub_category_name"]; 

               [array addObject:clam];// 
              } 

              pcs.myProductCategory.arrayClamshells = [NSMutableArray alloc] init]; //here it is crashing 

pcs.myProductCategory.arrayClamshells = [array copy]; 
             } 

             [pcsArray addObject:pcs]; 
            } 

            NSLog(@"PCs array is = %@", pcsArray); 
           } 

          }); 

但是當我初始化數組或任何值設置爲它導致崩潰。 錯誤消息:

-[__NSCFDictionary setArrayClamshells:]: unrecognized selector sent to instance 0x7f8f16059730 

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setArrayClamshells:]: unrecognized selector sent to instance 0x7f8f16059730' 
+0

哪裏是setArrayClamshells方法? – Rajesh

+0

@iDev它只是'.arrayClamshells ='時調用的默認setter方法。 – rebello95

+0

是的,它看起來像object.array或對象setArray –

回答

2

你的代碼在這裏

pcs.myProductCategory = [[MyProductCategory alloc] init]; 
pcs.myProductCategory = [item1 objectForKey:@"category"]; 

是正確創建MyProductCategory實例,但然後用字典來代替。你可能打算寫第二行

NSDictionary *category = [item1 objectForKey:@"category"]; 

,然後使用下一行解壓詞典內容

+0

謝謝@你的回答。 –

2

的錯誤是,你有一個正確初始化myProductCategory作爲您的自定義對象的行:

pcs.myProductCategory = [[MyProductCategory alloc] init]; 

但在非常下一行,你丟棄並將其設置爲可以在字典中通過category項規定:

pcs.myProductCategory = [item1 objectForKey:@"category"]; 

因此,當你到下面的行,它會崩潰,BEC澳洲英語myProductCategory不再是MyProductCategory,而是一個NSDictionary

pcs.myProductCategory.arrayClamshells = [NSMutableArray alloc] init]; 
+0

非常感謝@Rob ..現在它正在工作,建議+1。 –