keji
keji

Reputation: 5990

Convert Dictionaries into one array

This code:

for (NSDictionary *object in JSONArray) {
         NSMutableArray *birdtemp = [[NSMutableArray alloc] initWithObjects:object[@"case_name"], nil];
        [allparts addObject:birdtemp];
    }

outputs this log:

log: (
        (
        "NZXT Phantom 410"
    ),
        (
        "Thermaltake MK+"
    )
)

I want to know what code I could use to make that log into one array like:

log: (

    "NZXT Phantom 410",
    "Thermaltake MK+"
)

Json Array is:

log: (
        {
        "case_color" = White;
        "case_description" = "";
        "case_image" = "http://sitecom/get/parts/part_images/nzxtphantom410.jpeg";
        "case_name" = "NZXT Phantom 410";
        "case_price" = "99.99";
        "case_type" = ATX;
        id = 1;
    },
        {
        "case_color" = Black;
        "case_description" = "";
        "case_image" = "http://site.com/get/parts/part_images/thernaltake-mkplus.jpeg";
        "case_name" = "Thermaltake MK+";
        "case_price" = "84.99";
        "case_type" = ATX;
        id = 2;
    }
)

Upvotes: 1

Views: 6762

Answers (4)

keji
keji

Reputation: 5990

Troubleshooted and got this:

allparts = [[NSMutableArray alloc] init];
    NSString *birdtemp;
    for (NSDictionary *object in JSONArray) {
            birdtemp = object[@"case_name"];
        [allparts addObject:birdtemp];
    }

Upvotes: 1

Monolo
Monolo

Reputation: 18253

A simple way to obtain the result you are looking for is to use key-value coding (KVC):

NSArray *allparts = [JSONArray valueForKey:@"case_name"];

Key-value coding used on arrays in this way may seem a bit counter-intuitive at first, but it is very powerful.

Upvotes: 1

Prasad G
Prasad G

Reputation: 6718

   NSMutableArray *array = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in JSONArray) {

        [array addObject:[dict objectForKey:@"case_name"]];
    }
    NSLog(@"Your Array: %@",array);

I think it will be helpful to you.

Upvotes: 1

janusfidel
janusfidel

Reputation: 8106

NSMutableArray *birdtemp  = [NSMutableArray ....];   
     for (NSDictionary *object in JSONArray) {
               [birdtemp addObject:object[@"case_name"]];
      }
     [allparts addObject:birdtemp];

Upvotes: 0

Related Questions