我有一个叫做数组的字典:
{
"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"
}
我想将它们分开,以便它们是独一无二的.例如,应该只有一个名称,因此将有两个部分:foo和bar.
然后在每个名称(foo或bar)内应该有remaster_title(对于foo:和bar都是:和hoho)….
这应该是顺序:name> remaster_title>媒体>格式>编码.
我认为如果我的格式类似于:
music[name][remaster_title][media][format] = encoding
那么所有的密钥会合并吗?
我试过了:
+(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;
}
但NSLog(@“%@”,音乐);没有回报.
最佳答案 评论区域太小,无法提出几个问题,但让我们假设一些事情.您想要将JSON解析为对象树.让我们看看这段代码是否有帮助.
#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 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