我正在使用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
一切顺利,但在解析“设置”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
答案 0 :(得分:1)
看起来集合不是数组而是对象:
"sets":{