我正在使用 JSONModel JSON 解析器(很棒的库!),我在解析这个 JSON 响应时遇到了问题:http ://api.setlist.fm/rest/0.1/search/setlists.json?cityName=vilalba 有趣的 JSON响应中的代码是下一个:
{
"setlists":{
"@itemsPerPage":"20",
"@page":"1",
"@total":"5",
"setlist":[
{
"@eventDate":"12-04-2014",
"@id":"3bc38430",
"@lastUpdated":"2014-04-13T16:28:36.000+0000",
"@versionId":"53943b55",
"artist":{
"@disambiguation":"",
"@mbid":"8d1e76c8-80b0-44b9-9e86-25d148323a9e",
"@name":"Izal",
"@sortName":"Izal",
"url":"http:\/\/www.setlist.fm\/setlists\/izal-43dc3b23.html"
},
"venue":{
"@id":"73d64e85",
"@name":"Plaza de La Constitución",
"city":{
"@id":"3105522",
"@name":"Vilalba",
"@state":"Galicia",
"@stateCode":"58",
"coords":{
"@lat":"43.3",
"@long":"-7.6833333"
},
"country":{
"@code":"ES",
"@name":"Spain"
}
},
"url":"http:\/\/www.setlist.fm\/venue\/plaza-de-la-constitucion-vilalba-spain-73d64e85.html"
},
"sets":{
"set":{
"song":[
{
"@name":"Despedida"
},
{
"@name":"Hambre"
},
{
"@name":"Jenna Fisher"
},
{
"@name":"La mujer de verde"
}
]
}
},
"url":"http:\/\/www.setlist.fm\/setlist\/izal\/2014\/plaza-de-la-constitucion-vilalba-spain-3bc38430.html"
}
}
}
我的模型看起来是这样的:
@interface Concert : JSONModel
@property (nonatomic, strong) NSString *concertId;
@property (nonatomic, strong) ConcertArtist *concertArtist;
@property (nonatomic, strong) ConcertPlace *concertPlace;
@property (nonatomic, strong) NSString *concertDate;
@property (nonatomic, strong) NSArray<Optional, ConcertSet> *concertSets;
@property (nonatomic) NSNumber<Ignore> *concertSongsNumber;
@end
@interface ConcertSet : JSONModel
@property (nonatomic, strong) NSDictionary<ConcertSong,Optional> *setSongs;
@property (nonatomic) NSNumber<Optional> *setNumEncore;
@end
一切正常,但是在解析“sets”JSON 代码时,出现下一个错误:
Invalid JSON data. The JSON type mismatches the expected type. Check the error user information.
kJSONModelTypeMismatch=Property 'concertSets' is declared as NSArray<ConcertSet>* but the corresponding JSON value is not a JSON Array.
kJSONModelKeyPath=concerts.concertSets}
我在我的代码中找不到错误,我正在考虑它的某种 JSON 数据格式错误。在那种情况下,可能的解决方案是什么?提前致谢!巴勃罗·布兰科
更新:正如 Ican Zilb 所说,“音乐会”和“集合”之间还有另一个对象:“集合”。如下更新我的类,解析完美。谢谢!
@interface Concert : JSONModel
@property (nonatomic, strong) NSString *concertId;
@property (nonatomic, strong) ConcertArtist *concertArtist;
@property (nonatomic, strong) ConcertPlace *concertPlace;
@property (nonatomic, strong) NSString *concertDate;
@property (nonatomic, strong) ConcertSets<Optional> *concertSets;
@property (nonatomic) NSNumber<Ignore> *concertSongsNumber;
@end
@protocol ConcertSets
@end
@interface ConcertSets : JSONModel
@property (nonatomic, strong) NSArray<ConcertSet,Optional> *concertSets;
@end
@protocol ConcertSet
@end
@interface ConcertSet : JSONModel
@property (nonatomic, strong) NSArray<ConcertSong,Optional> *setSongs;
@property (nonatomic) NSNumber<Optional> *setNumEncore;
@end