4

I have a dictionary called array:

     {
        "encoding": "320", 
        "format": "MP3", 
        "media": "CD", 
        "name": "foo", 
        "remaster_title": ""
    }, 
    {
        "encoding": "V0 (VBR)", 
        "format": "MP3", 
        "media": "CD", 
        "name": "bar", 
        "remaster_title": ""
    }, 
    {
        "encoding": "Lossless", 
        "format": "FLAC", 
        "media": "CD", 
        "name": "bar", 
        "remaster_title": "hoho"
    }

I want to separate these so that they are unique. For example there should only be one name so there will be two sections: foo and bar.

Then within each name (foo or bar) there should be the remaster_title (for foo: and bar both: and hoho)....

This should be in the order: name > remaster_title > media > format > encoding.

I think this would work if I could have a format similar to:

music[name][remaster_title][media][format] = encoding

so all the keys would then merge?

I have tried:

+(NSMutableDictionary*)handleReturnedArtistJson:(NSDictionary*)json{
    NSMutableDictionary *music = [[NSMutableDictionary alloc] init];
    for(NSDictionary *result in json[@"response"][@"group"]) {
        NSString* release_type = result[@"releaseType"];
        NSString* name = result[@"groupName"];

        if (![release_type  isEqual: @"Compilation"]){
            for (NSDictionary *subgroup in result[@"subgroup"]){
                NSString* media = subgroup[@"media"];
                NSString* remaster_title = subgroup[@"remasterTitle"];
                NSString* format = subgroup[@"format"];
                NSString* encoding = subgroup[@"encoding"];

                music[name][remaster_title][media][format] = encoding;
            }
        }
    }
    return music;
}

but NSLog(@"%@",music); returns nothing.

3
  • 1
    I don't see why you want all the nested dictionaries, why not just have something like {name:{remastered_title1:{"encoding":...,"format":...,"media":...}, remasted_title2:{...}}, name2:...} ? Commented Aug 30, 2016 at 16:53
  • As Tadhg has said, is there a reason you need it in a multi-nested dictionary? Can data be passed to your app in that nested format, if so you could potentially use Mantle, I can provide an example of this, if that's what you're looking for. Commented Sep 6, 2016 at 21:51
  • I just want to be able to iterate through each of the arrays within each array :) I am so confused about the structure! Commented Sep 7, 2016 at 8:33

1 Answer 1

1

The comment area is too small to ask a few questions, but let's assume a few things. You want to parse the JSON into an object tree. Let's see if this code can help.

#import <Foundation/Foundation.h>

typedef NSMutableDictionary<NSString*, NSString*> formatDictionary;
typedef NSMutableDictionary<NSString*, formatDictionary*> mediaDictionary;
typedef NSMutableDictionary<NSString*, mediaDictionary*> titleDictionary;
typedef NSMutableDictionary<NSString*, titleDictionary*> nameDictionary;

@interface DataSource : NSObject
- (nameDictionary*)parseJSONFile:(NSString *)jsonFile error:(NSError *__autoreleasing *)outError;
@end

Implementation

@implementation DataSource

- (NSString *)valueOrDefaultFromDictionary:(NSDictionary *) dictionary forKey: (NSString *) key
{
    NSString * value = dictionary[key];
    if (![value length]) {
        return @"";
    }
    return value;
}

- (NSArray *)jsonToArray:(NSString *)jsonFile error:(NSError *__autoreleasing *)outError
{
    NSString *path  = [[NSBundle mainBundle] pathForResource:jsonFile ofType:@"json"];
    NSString *jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSError *jsonError;
    NSArray *jsonDataArray = [NSArray new];
    jsonDataArray = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];

    if (jsonDataArray == nil) {
        if (outError) {
            *outError = [NSError errorWithDomain:@"yourdomain" code:-42 userInfo:@{NSUnderlyingErrorKey: jsonError}];
        }
        return nil;
    }
    return jsonDataArray;
}


- (nameDictionary*)parseJSONFile:(NSString *)jsonFile error:(NSError *__autoreleasing *)outError
{
    NSArray * jsonDataArray = [self jsonToArray:jsonFile error:outError];
    if (!jsonDataArray) {
      return nil;
    }
    nameDictionary* music = [nameDictionary new];

    [jsonDataArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString * name = [self valueOrDefaultFromDictionary:obj forKey:@"name"];
        NSString * title = [self valueOrDefaultFromDictionary:obj forKey:@"remaster_title"];
        NSString * media = [self valueOrDefaultFromDictionary:obj forKey:@"media"];
        NSString * format = [self valueOrDefaultFromDictionary:obj forKey:@"format"];
        NSString * encoding = [self valueOrDefaultFromDictionary:obj forKey:@"encoding"];
        music[name] = [@{title: [@{media: [@{format:encoding} mutableCopy] } mutableCopy] } mutableCopy];
    }];
    return music;
}
@end
Sign up to request clarification or add additional context in comments.

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.