Getting error in JSON parsing with array in iOS

102 views Asked by At

I am newly to iPhone, basically in my application I am parsing below JSON model and need to save it in my custom Entity class.

JSON model :

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

I have created three entity classes like :

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

Here is my code for parsing data :

    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);
                                }

                            });

But it is crashing when I am initializing the array or setting any value to it. Error message is :

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

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

There are 2 answers

0
Wain On BEST ANSWER

Your code here

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

Is correctly creating your MyProductCategory instance, but then replacing it with a dictionary. You probably intended to write the second line as

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

And then use that in the next line to unpack the dictionary content

0
Rob On

The error is that you have a line that properly initializes myProductCategory as your custom object:

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

But in the very next line, you discard that and set it to be the dictionary specified by category entry:

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

Thus, when you get to the following line, it would crash, because myProductCategory is no longer a MyProductCategory, but rather is a NSDictionary:

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