0

I have an array from a plist and each value contains an key and a string and a secondary array that I get from a json file online. I want to order the secondary array based on the keys in the first array.

I want to achieve something like this:

array1: Item0 - EUR - String Item1 - USD - String Item2 - AUD - String etc

array2: Item0 - AUD - 123.242 Item1 - EUR - 535.123 Item2 - USD - 325.646 etc

I have the same key index on both but I want to get the value for the key index from array2 based on the order of the key index in array1.

I have researched online but I cannot find a suitable solution that I can understand how to implement it.

How can I implement this?

Here is the plist file - https://gist.github.com/iulianvarzaru/11c400ba1edf4a165082

And the json file - https://gist.github.com/iulianvarzaru/1915e02a9201c57f49b3

5
  • what is the key, what the value? Commented Nov 9, 2013 at 16:24
  • The key AUD, EUR, USD etc ( these valus I get from Json file) and evey key has asigned a value. Now I have created a plist file to re-ordered this keys in different way. I want to reorder the json array to retrieve the value for each key. Commented Nov 9, 2013 at 16:26
  • Do you really care about the order of array1 or are you just trying to get the description from the currency code? Commented Nov 9, 2013 at 16:37
  • Yes, I need the order to be in that specific way because the order in the json file its not always the same. Commented Nov 9, 2013 at 16:39
  • The JSON file does not contain an array, it contains a dictionary which has no order. Commented Nov 9, 2013 at 16:42

3 Answers 3

1

Given that the JSON file you've linked to doesn't contain an array but a dictionary, you can simply iterate over array1 from the plist file. Each element of that array is a dictionary with a "Cod" key and a "Descriere" key. Get the value for the "Cod" key and then simply use that value as the key into the dictionary from the JSON file.

NSDictionary* jsonFileDict = ...;
NSDictionary* jsonFileInnerDict = jsonFileDict[@"rate"];
for (NSDictionary* dict in array1)
{
    NSString* code = dict[@"Cod"];
    NSNumber* jsonNumber = jsonFileInnerDict[code];
    // Do something with jsonNumber
}
Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like these are key-value pairs, in which case, you can convert it to a Map, and then do direct lookups.

If you can manipulate the JSON file as JSON, then it reduces a conversion, but may not be the most efficient implementation.

Caveats:

  1. This method assumes that you wont have key overloading (which is possible in a numeric array, but not in a map)
  2. This requires a conversion from one data structure to another

EDIT: (due to increased information by OP).

The JSON file you receive doesn't contain an array, it contains an object. Thus, all the values are direct-lookup. So, you can traverse your array in Obj-c, and directly access the corresponding values in the JSON.

Sorry about the lack of actual code-samples.

5 Comments

are you talking about objective-c?
The concept is language agnostic. It would be best implemented in Obj-c with a NSMutableDictionary
the question is about objective-c. cocoa has no map, numeric array or key overloading. the is question targets on the real API.
The JSON file you receive doesn't contain an array, it contains an object. An array is an object too. it contains a dictionary. and not all objects have direct look up.
oops, silly me. Anyway, your answer should be the accepted answer. It does what I was trying to explain, but with code examples.
0

You are dealing with a dictionary in the response, not an array.

You should transform it to something like

{ 
    @"currency": @"EUR",
    @"value": 123.45
}

create and sort it it like

NSArray *keys = @[@"EUR",@"USD",@"AUD"];
NSDictionary *dict = @{@"AUD":@(123.242), @"EUR": @(535.123), @"USD": @(325.646)};
NSMutableArray *result = [@[] mutableCopy];

for (NSString *key in keys) {
    [result addObject:@{@"value":dict[key], @"currency": key}];
}
NSLog(@"%@", result);

(
        {
        currency = EUR;
        value = "535.123";
    },
        {
        currency = USD;
        value = "325.646";
    },
        {
        currency = AUD;
        value = "123.242";
    }
)

Or write a model class that can handle this information.

Comments

Your Answer

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