如何在Objective C中解析此JSON?我一直在使用jsonmodel.com的代码进行解析。
{
"found":10958,
"start":3141,
"hits":[
{
"pid":"76493",
"title":"Beton Armu00e9",
"artist":"Raiden",
"genre":"Dubstep",
"image":"A76493_BetonArm_BetonArm.jpg",
"label":"Offkey",
"year":"2011",
"price":9.99,
"release":"Beton Armu00e9",
"type":"Album",
"tracks":{
"0":{
"name":"Barbican",
"file":"A76481_Barbican.mp3",
"tracknum":1,
"pid":"76481"
},
"1":{
"name":"Trinity",
"file":"A76482_Trinity.mp3",
"tracknum":2,
"pid":"76482"
},
"2":{
"name":"Tricorn",
"file":"A76483_Tricorn.mp3",
"tracknum":3,
"pid":"76483"
},
"3":{
"name":"Brutalist",
"file":"A76484_Brutalist.mp3",
"tracknum":4,
"pid":"76484"
},
"4":{
"name":"Trellick",
"file":"A76485_Trellick.mp3",
"tracknum":5,
"pid":"76485"
}
}
}
]
}
JSONModel需要一个指针字符串来声明键,但这里的键是数字。这是我需要的,但不起作用:
#import "JSONModel.h"
#import "songParentModel.h"
@protocol albumModel @end
@interface albumModel : JSONModel
@property (strong,nonatomic) NSString *title;
@property (strong,nonatomic) NSString *image;
@property (strong,nonatomic) NSString *artist;
@property (strong,nonatomic) songParentModel *0; // THIS DOESN'T WORK (of course)
@end
我只需要获得第一首曲目,但知道如何全部获取它们会很棒。
我对解决方案的最佳猜测是停止使用JSONModel并使用其他一些简化方法解析JSON。
答案 0 :(得分:-1)
创建一个这样的类:
标题:
#import "JSONModel.h"
@protocol Track @end
@interface Track : JSONModel
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) NSString* file;
@property (assign, nonatomic) int tracknum;
@property (strong, nonatomic) int pid;
@end
将实施保留为默认值。 现在在您的模型中添加此属性:
@property (strong, nonatomic) NSArray<Track>* allTracks;
并更改+(JSONKeyMapper*)keyMapper
的实施,并将以下项目添加到您的字典中。
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{
@"tracks":@"allTracks",
}];
}
通过这种方式,您可以获得所有曲目的数组,并且您还可以获得每个曲目的所有详细信息。