How to store JSON Dictionary response in NSMutableArray

775 views Asked by At

This is my response ... My dictnary name is "msg"

{
Result =     {
    Data =         {
        engine =             {
            service =                 {
                data =                     (
                                            {
                        checked = 1;
                        field = 1;
                        name = Ettersyn;
                    },
                                            {
                        checked = 1;
                        field = 1;
                        name = "Med Alpha VP";
                    },
                                            {
                        checked = 0;
                        field = 0;
                        name = "Med B.IMP.";
                    }
                );
                "engine_service_id" = 1;
                "engine_service_name" = "4 Cyl";
            };
            type =                 {
                "engine_id" = 1;
                "engine_name" = Benzin;
            };
        };
        tact =             {
            service =                 {
                data =                     (
                                            {
                        checked = 0;
                        field = 1;
                        name = "Little ettersyn";
                    },
                                            {
                        checked = 0;
                        field = 1;
                        name = "Short ettersyn";
                    }
                );
                "tact_service_id" = 2;
                "tact_service_name" = "21-50 hk";
            };
            type =                 {
                "tact_id" = 1;
                "tact_name" = "2 Takt";
            };
        };
    };
    status = success;
};
}

I try to to store the data like this

var arrayMain : NSMutableArray = msg.valueForKey("Result")?.valueForKey("Data") as NSMutableArray

My problem is i got "EXC_BAD_ACCESS" error While running ... Am also check allocation of array ... I try to store it in NSMutableDictionary its work perfectly ... How can i store this response in NSMutableArray ?

2

There are 2 answers

0
Mike On

If you look carefully at the response data structure you'll see the problem.

This first operation: msg.valueForKey("Result") would return an dictionary with two key/values; a "Data" dictionary, and "Status" string.

Your second operation .valueForKey("Data") would return a dictionary with two key/values; an "Engine and "Tact" dictionary.

Because it's a dictionary, you can create a dictionary from it, which is what you said worked, or you're going to have to rethink how you want to store if if indeed you want it in an array, but you can't create an array directly from a dictionary - they are different data structures.

0
Himanshu gupta On

You cannot assign Dictionary object to Array, that's why it is giving the error.

But you can store that dictionary to array as:

var arrayMain : NSMutableArray = NSMutableArray()

arrayMain.addObject(msg.valueForKey("Result")?.valueForKey("Data"))