{ "enCoding": "320","format": "MP3","media": "CD","name": "foo","remaster_Title": "" },{ "enCoding": "V0 (VBR)","name": "bar",{ "enCoding": "Lossless","format": "FLAC","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总结
以上是内存溢出为你收集整理的如何在objective-c中处理json格式的嵌入式数组全部内容,希望文章能够帮你解决如何在objective-c中处理json格式的嵌入式数组所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)