0

I have a json string from server, at that string a dictionary with array of dictionaries. Each dictionary represent an object, that i need to convert into NSString and take md5 hash from that. So it's looks like that: - dict -> array -> obj1 (dict), obj2 (dict) e.t.c ... It's all good, but in json string from server i have a given order. So when i get obj dictionary from array, order is incorrect, and, of course, my md5 of each object is not equal to md5 from server, that was made on server.

Is there a choice to save order when convert json string into NSDictionary ? I'am also have tried M13OrderedDictionary lib, but with no success. Code of what have i do:

- (void)mainMethod
{
    NSDictionary *dict = [jsonString objectFromJSONString]; 
    NSArray *arr = dict[@"params"];
    for (NSDictionary *obj in arr)
    {
          NSString *jsonString = [package JSONString];
          NSString *md5 = [jsonString MD5];
          [arrayOfMD5 addObject:md5];
    }
}

// Categories in other classes
- (id)objectFromJSONString
{
    NSError *serializationError = nil;
    id result = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
                                                   options:0
                                                     error:&serializationError];
    if (serializationError)
    {
        DDLogError(@"Failed unserialising JSON: %@", serializationError.localizedDescription);
    }
    return result;
}

- (NSString *) JSONString
{
    NSError *serializationError = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&serializationError];
    if (serializationError)
    {
        DDLogError(@"Failed serialising JSON: %@", serializationError.localizedDescription);
    }
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
user3312949
  • 145
  • 1
  • 9
  • 4
    NSDictionary is not an ordered collection. There's no way to control how it orders things, and it may change completely depending on OS version, device type, and dictionary contents. – gronzzz Jan 26 '15 at 20:18
  • Likewise, a JSON "object" is not ordered. – Hot Licks Jan 26 '15 at 20:44

0 Answers0