0

I'm trying to save some specific JSon data and store them in a simple NSArray.

This is the JSon I have:

products = {
    72 =     {
        value_a = "something1";
        value_b = "something2";
        value_c = "something3";
    };

    73 =     {
        value_a = "something1";
        value_b = "something2";
        value_c = "something3";
    };

    74 =     {
        value_a = "something1";
        value_b = "something2";
        value_c = "something3";
    };
    [etc]
}

This is how I assign the JSon data in a NSMutableArray.

NSMutableArray *array = [json objectForKey:@"products"];

What I need is to store, let's say, just value_b of each child in an array. Any suggestion?

2 Answers 2

4

What you have here is a dictionary containing key/values, where values are also dictionaries. If you need to have an array containing, say value_b, of each of these dictionaries you can have it like this:

NSDictionary *products = [json objectForKey:@"products"];
NSMutableArray *b_values = [[NSMutableArray alloc] initWithCapacity:0];

for (NSDictionary *product in [products allValues])
{
  [b_values addObject:[product valueForKey:@"value_b"]];
}

// Now you have an array with all "value_b" objects
Sign up to request clarification or add additional context in comments.

5 Comments

Just a little extra question, @Alladinian. What I don't have always the same name ("value_b" in the previous example) but different things? Let's say the NSMutableArray contains values/keys as following: link How do I extract data?
@aur0n Do you mean that there is a case that you will not know the names of the keys? Because if you know the names, you just do valueForKey:@"foo" (for example) and you're done...
Yeah, I mean if I don't know the names of the keys.
@aur0n The link that you've posted is a dictionary not an array. In that case you get an NSArray with all the keys (foo, bar, dunno...) with: [products allKeys] and an NSArray with all the values (13,9.8,9.8...) with: [products allValues] assuming of course that you have this information stored in an NSDictionary named "products". Also you should really read Apple's documentation on Collections
Could you please add the Swift code of this answer!!
1

I dont know what you are using, But I can suggest you to use SBJsonFramework

This kit has very convenient methods to store json data. Store all Json Data in

NSMutableDictionary

Then for each object you can extract specific value using key to store them in

NSMutableArray

2 Comments

Hi @hp iOS Cover, thanks for your quick reply. I'm using iOS 5 native JSon functions, I don't think I need to use the Framework you suggested, because the NSMutableArray I assign in the last piece of code is (I think) already composed of dictionary keys, but I'm not capable of extracting what I need. Can you help me with that?
If you are using NSJSONSerialization then, I would suggets that check if you crated the json data & json objects properly. Bcoz that class also has methods with convenient return types to catch the data accordingly In your problem it seems that 'products' is json DATA & 72,73,73 are json OBJECTS.

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.