我正在使用JSONModel将 Instagram 信息从 JSON 解析为我的 iOS 应用程序中的模型。我尝试了很多方法来实现它,但我做不到。响应的三个主要部分是数据、元数据和分页。我已经创建了模型,模型始终为 NULL。我将附上我试图解析的响应、我的模型以及我如何处理响应。我认为这可能是结构的问题,但我不知道我做错了什么。谢谢你的帮助。
Instagram 的回应
{
"data": [
{
"attribution": null,
"caption": {
"created_time": "1387987595",
"from": {
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam"
},
"id": "618807262892627401",
"text": "Navidad :)"
},
"comments": {
"count": 2,
"data": [
{
"created_time": "1387987956",
"from": {
"full_name": "Luisa Bellydancer",
"id": "548519361",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_548519361_75sq_1398196543.jpg",
"username": "luisacantillo"
},
"id": "618810289837938356",
"text": "Te amo beb\u00e9"
},
{
"created_time": "1403666639",
"from": {
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam"
},
"id": "750332614214522476",
"text": "#sacanamboy"
}
]
},
"created_time": "1387987595",
"filter": "Walden",
"id": "618807262548694878_268167404",
"images": {
"low_resolution": {
"height": 306,
"url": "http://scontent-b.cdninstagram.com/hphotos-xpf1/t51.2885-15/1515365_559830227444878_1414305160_a.jpg",
"width": 306
},
"standard_resolution": {
"height": 640,
"url": "http://scontent-b.cdninstagram.com/hphotos-xpf1/t51.2885-15/1515365_559830227444878_1414305160_n.jpg",
"width": 640
},
"thumbnail": {
"height": 150,
"url": "http://scontent-b.cdninstagram.com/hphotos-xpf1/t51.2885-15/1515365_559830227444878_1414305160_s.jpg",
"width": 150
}
},
"likes": {
"count": 2,
"data": [
{
"full_name": "Luisa Bellydancer",
"id": "548519361",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_548519361_75sq_1398196543.jpg",
"username": "luisacantillo"
},
{
"full_name": "Steven Zambrano",
"id": "198842014",
"profile_picture": "http://photos-d.ak.instagram.com/hphotos-ak-xpa1/10354559_1419915091614315_879045002_a.jpg",
"username": "steven_zambrano"
}
]
},
"link": "http://instagram.com/p/iWce08pZde/",
"location": null,
"tags": [
"sacanamboy"
],
"type": "image",
"user": {
"bio": "",
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam",
"website": ""
},
"users_in_photo": [
{
"position": {
"x": 0.39722222,
"y": 0.15416667
},
"user": {
"full_name": "Luisa Bellydancer",
"id": "548519361",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_548519361_75sq_1398196543.jpg",
"username": "luisacantillo"
}
},
{
"position": {
"x": 0.55138886,
"y": 0.09583333
},
"user": {
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam"
}
}
]
},
{
"attribution": null,
"caption": {
"created_time": "1391910835",
"from": {
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam"
},
"id": "651717783438333290",
"text": "Vista de Bogot\u00e1"
},
"comments": {
"count": 1,
"data": [
{
"created_time": "1403665438",
"from": {
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam"
},
"id": "750322542532990878",
"text": "#sacanamboy"
}
]
},
"created_time": "1391910835",
"filter": "Hudson",
"id": "651717782742079012_268167404",
"images": {
"low_resolution": {
"height": 306,
"url": "http://scontent-b.cdninstagram.com/hphotos-xfp1/t51.2885-15/1889287_611340528913277_403885343_a.jpg",
"width": 306
},
"standard_resolution": {
"height": 640,
"url": "http://scontent-b.cdninstagram.com/hphotos-xfp1/t51.2885-15/1889287_611340528913277_403885343_n.jpg",
"width": 640
},
"thumbnail": {
"height": 150,
"url": "http://scontent-b.cdninstagram.com/hphotos-xfp1/t51.2885-15/1889287_611340528913277_403885343_s.jpg",
"width": 150
}
},
"likes": {
"count": 3,
"data": [
{
"full_name": "Luisa Bellydancer",
"id": "548519361",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_548519361_75sq_1398196543.jpg",
"username": "luisacantillo"
},
{
"full_name": "Stiven Sepulveda Casta\u00f1o",
"id": "270408040",
"profile_picture": "http://photos-a.ak.instagram.com/hphotos-ak-xfp1/10354393_1459814907595448_1770710968_a.jpg",
"username": "johntato"
},
{
"full_name": "Julian Garcia",
"id": "318551590",
"profile_picture": "http://photos-e.ak.instagram.com/hphotos-ak-xaf1/10413240_284995708339980_626057267_a.jpg",
"username": "juliangarcia20"
}
]
},
"link": "http://instagram.com/p/kLXd7WJZYk/",
"location": {
"id": 1187760,
"latitude": 4.67413252,
"longitude": -74.037993338,
"name": "Mirador de La Calera, Bogota"
},
"tags": [
"sacanamboy"
],
"type": "image",
"user": {
"bio": "",
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam",
"website": ""
},
"users_in_photo": [
{
"position": {
"x": 0.3625,
"y": 0.54444444
},
"user": {
"full_name": "Luisa Bellydancer",
"id": "548519361",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_548519361_75sq_1398196543.jpg",
"username": "luisacantillo"
}
},
{
"position": {
"x": 0.60694444,
"y": 0.42222223
},
"user": {
"full_name": "Camilo Sacanamboy",
"id": "268167404",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_268167404_75sq_1386784112.jpg",
"username": "csacanam"
}
}
]
}
],
"meta": {
"code": 200
},
"pagination": {
"deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead",
"min_tag_id": "1403666639684031",
"next_min_id": "1403666639684031"
}
}
楷模
@interface TagPicturesInstagram : JSONModel
@property (strong,nonatomic) NSArray<PictureInstagram> *data;
@property (strong,nonatomic) MetaInstagram *meta;
@property (strong,nonatomic) PaginationInstagram *pagination;
@end
@protocol PictureInstagram
@end
@interface PictureInstagram : JSONModel
@property (strong,nonatomic) NSString<Optional> *attribution;
@property (strong,nonatomic) CaptionInstagram *caption;
@property (strong,nonatomic) CommentsInstagram *comments;
@property (strong,nonatomic) NSString *created_time;
@property (strong,nonatomic) NSString *filter;
@property (strong,nonatomic) NSString *id;
@property (strong,nonatomic) ImagesInstagram *images;
@property (strong,nonatomic) LikesInstagram *likes;
@property (strong,nonatomic) NSString *link;
@property (strong,nonatomic) LocationInstagram<Optional> *location;
@property (strong,nonatomic) NSArray<Optional> *tags;
@property (strong,nonatomic) NSString *type;
@property (strong,nonatomic) UserInstagram *user;
@property (strong,nonatomic) NSArray<UserInPhoto> *users_in_photo;
//@property (assign,nonatomic) BOOL user_has_liked;
//@property (strong,nonatomic) NSString<Optional> *website;
@end
@interface MetaInstagram : JSONModel
@property (assign,nonatomic) int code;
@property (strong,nonatomic) NSString<Optional> *error_type;
@property (strong,nonatomic) NSString<Optional> *error_message;
@end
@interface PaginationInstagram : JSONModel
@property (strong,nonatomic) NSString<Optional> *next_max_tag_id;
@property (strong,nonatomic) NSString<Optional> *deprecation_warning;
@property (strong,nonatomic) NSString<Optional> *next_max_id;
@property (strong,nonatomic) NSString<Optional> *next_min_id;
@property (strong,nonatomic) NSString<Optional> *min_tag_id;
@property (strong,nonatomic) NSString<Optional> *next_url;
@end
@interface CaptionInstagram : JSONModel
@property (strong,nonatomic) NSString *created_time;
@property (strong,nonatomic) FromInstagram *from;
@property (strong,nonatomic) NSString *id;
@property (strong,nonatomic) NSString<Optional> *text;
@end
@interface CommentsInstagram : JSONModel
@property (assign,nonatomic) int count;
@property (strong,nonatomic) NSArray<CommentInstagram> *data;
@end
@interface ImagesInstagram : JSONModel
@property (strong,nonatomic) ImgInstagram *low_resolution;
@property (strong,nonatomic) ImgInstagram *standard_resolution;
@property (strong,nonatomic) ImgInstagram *thumbnail;
@end
@interface LikesInstagram : JSONModel
@property (assign,nonatomic) int count;
@property (strong,nonatomic) NSArray<FromInstagram> *data;
@end
@interface LocationInstagram : JSONModel
@property (assign,nonatomic) int id;
@property (assign,nonatomic) double latitude;
@property (assign,nonatomic) double longitude;
@property (strong,nonatomic) NSString *name;
@end
@interface UserInstagram : JSONModel
@property (strong,nonatomic) NSString<Optional> *bio;
@property (strong,nonatomic) NSString *full_name;
@property (strong,nonatomic) NSString *id;
@property (strong,nonatomic) NSString *profile_picture;
@property (strong,nonatomic) NSString *username;
@property (strong,nonatomic) NSString<Optional> *website;
@end
@interface UserInPhoto : JSONModel
@property (strong,nonatomic) PositionTagInstagram *position;
@property (strong,nonatomic) FromInstagram *user;
@end
@protocol FromInstagram
@end
@interface FromInstagram : JSONModel
@property (strong,nonatomic) NSString *full_name;
@property (strong,nonatomic) NSString *id;
@property (strong,nonatomic) NSString *profile_picture;
@property (strong,nonatomic) NSString *username;
@end
@protocol CommentInstagram
@end
@interface CommentInstagram : JSONModel
@property (strong,nonatomic) NSString *created_time;
@property (strong,nonatomic) FromInstagram *from;
@property (strong,nonatomic) NSString *id;
@property (strong,nonatomic) NSString *text;
@end
@protocol ImgInstagram
@end
@interface ImgInstagram : JSONModel
@property (assign,nonatomic) double height;
@property (strong,nonatomic) NSString *url;
@property (assign,nonatomic) double width;
@end
@interface PositionTagInstagram : JSONModel
@property (assign,nonatomic) double x;
@property (assign,nonatomic) double y;
@end
通过AFNetworking获得响应
-(void) loadInstagramInfo
{
//1. Create request
NSString *string = [NSString stringWithFormat:@"%@", BaseURLString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2. Do request with AFNetworking
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *json = [responseObject description];
NSError *err = nil;
self.pictures = [[TagPicturesInstagram alloc]initWithString:json error:&err];
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error cargando fotos");
/*
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];*/
}];
[operation start];
}