JSON 解析 iOS 中的数组时出错

Getting error in JSON parsing with array in iOS

我是 iPhone 的新手,基本上在我的应用程序中,我在 JSON 模型下进行解析,需要将其保存在我的自定义实体 class.

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"
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

我创建了三个实体 class,例如:

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

Clamshell.h :

#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'

错误是您有一行将 myProductCategory 正确初始化为您的自定义对象:

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

但是在下一行中,您将其丢弃并将其设置为 category 条目指定的字典:

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

因此,当您到达下一行时,它会崩溃,因为 myProductCategory 不再是 MyProductCategory,而是 NSDictionary:

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

你的代码在这里

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

正在正确创建您的 MyProductCategory 实例,但随后将其替换为字典。您可能打算将第二行写为

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

然后在下一行中使用它来解压字典内容