1

I have to read some values from nested JSON and I can't read them properly. JSON looks like this:

 "addons" : [
      {
        "group_title" : "Veggie Toppings",
        "group_type" : "T",
        "item_id" : "29",
        "addon" : [
          {
            "id" : "31",
            "item_title" : "Ham"
          },
          {
            "id" : "32",
            "item_title" : "Mushrooms"
          }
        ]
      },
      {
        "group_title" : "Meat Toppings",
        "group_type" : "T",
        "item_id" : "33",
        "addon" : [
          {
            "id" : "30",
            "item_title" : "Sausage"
          }
        ]
      }

And my code is:

-(void)setData:(NSMutableDictionary *)menuItems{
    self.menuItem = menuItems;
    // [GSDVActivityIndicator stopWithID:kNotificationHello];
    self.dictionaryOrder = [NSMutableDictionary dictionaryWithDictionary:[self.menuItem valueForKey:@"order"]];
    self.arrayPrices = [NSArray arrayWithArray:[self.menuItem valueForKey:@"price"]];
    self.arrayAddons = [NSArray arrayWithArray:[self.menuItem valueForKey:@"addons"]];
    self.myAddons = [NSArray arrayWithArray:[self.arrayAddons valueForKey:@"addon"]];

    [self setViewGUIData];

    NSLog ( @"addons= %@", [self.arrayAddons valueForKey:@"Group_title]);

    NSLog ( @"addon = %@", [self.myAddons valueForKey:@"item_title"]);

}

And the result is:

2014-03-24 19:48:04.446 [3698:70b] addons= (
    "Veggie Toppings",
    "Meat Toppings"
)
2014-03-24 19:48:04.447 [3698:70b] addon = (
        (
        Ham,
        Mushrooms
    ),
        (
        "Sausage"
    )
)

SO problem is in the second part where the items "ham, mushrooms and sausage" are in bad format and i can't read them and put them in label. Obviously this line is bad :

self.myAddons = [NSArray arrayWithArray:[self.arrayAddons valueForKey:@"addon"]];

But i don't know how to correct it.

Thanks.

2
  • 1
    In second nslog you have "itemtitle" but key in dictionary is : "item_title", how it works? Commented Mar 24, 2014 at 19:23
  • the key names were in my native language so i edited them for the post, but that is not the problem, it works in Xcode, i just misspelled in this post. Commented Mar 24, 2014 at 19:33

2 Answers 2

1

What you're doing is working correctly. The values for "addon" are [{"id" : "31", "item_title" : "Ham"}, {"id" : "32", "item_title" : "Mushrooms"}] and {"id" : "30","item_title" : "Sausage"}, so valueForKey is giving you an array containing those two values.

You don't say exactly what format you want, but I'm guessing you want a flat list. You can just make an array, iterate over the groups, and put in each addon:

NSMutableArray* addons = [NSMutableArray array];
NSArray* groups = [self.menuItem valueForKey:@"addons"];
[groups enumerateObjectsUsingBlock:^(NSDictionary* group, NSUInteger idx, BOOL *stop) {
    NSArray* addonsInThisGroup = group[@"addon"];
    [addonsInThisGroup enumerateObjectsUsingBlock:^(NSDictionary* addon, NSUInteger idx, BOOL *stop) {
        [addons addObject:addon];
    }];
}];

This would result in:

[
    {"id" : "31", "item_title" : "Ham"}, 
    {"id" : "32", "item_title" : "Mushrooms"}, 
    {"id" : "30","item_title" : "Sausage"}
]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much:D now i get addon = ( Ham, Mushrooms, "Ameci Sausage" ) Do you know why are Ham and mushrooms without quotes?
When displaying strings in the console, it will use quotes only if it needs to. Ameci Sausage gets them because it has a space.
1

Add JSONKit to your project files and turn JSON strings into dictionay objects. Let's pretend your json datas are stored in a varaible called "responseString". You just have to do this:

        NSArray *addons = [[NSArray alloc] init];
        addons = [[responseString objectFromJSONString] objectForKey: @"addons"];

Now in your array addons you'll have dictionary objects with keys "group_title", "group_type", "item_id" and "addon". Inside the object with the key "addon" you'll have an array of two dictionary objects with the keys "id" and "item_title".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.