我正在使用它的API制作我团队项目的原生iOS应用程序。 API有三种主要方法:获取用户的个人资料信息,日记和证书表。最后一个方法使用表的HTML代码返回给我JSON数据。看起来像这样
{"html":"<table class=\"table term-marks\" cellspacing=\"0\" cellspadding=\"0\"> <thead> <tr> <td>\u041f\u0440\u0435\u0434\u043c\u0435\u0442<\/td> <td colspan=\"10\">\u041e\u0446\u0435\u043d\u043a\u0438<\/td><td width=\"60px\">\u0421\u0440\u0435\u0434\u043d\u0438\u0439 <br\/>\u0431\u0430\u043b\u043b<\/td> <td width=\"60px\">\u0421\u0440\u0435\u0434\u043d\u0438\u0439 <br\/>\u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0443<\/td> <td width=\"60px\">\u0421\u0440\u0435\u0434\u043d\u0438\u0439 <br\/>\u043f\u043e \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438<\/td><td width=\"60px\">\u0418\u0442\u043e\u0433\u043e\u0432\u0430\u044f \u043e\u0446\u0435\u043d\u043a\u0430<\/td> <\/tr> <\/thead> <tbody> <tr> <td>\u0410\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439<\/td> <td>5<\/td> <td>5<\/td> <td>5<\/td> <td>4<\/td> <td>4<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.60<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0411\u0438\u043e\u043b\u043e\u0433\u0438\u044f<\/td> <td>3<\/td> <td>5<\/td> <td>5<\/td> <td>5<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.50<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0413\u0435\u043e\u0433\u0440\u0430\u0444\u0438\u044f<\/td> <td>5<\/td> <td>4<\/td> <td>3<\/td> <td>2<\/td> <td>3<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>3.40<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u043a\u0430 \u0438 \u0418\u041a\u0422<\/td> <td>5<\/td> <td>5<\/td> <td>4<\/td> <td>5<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.75<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0418\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u043e<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0418\u0441\u0442\u043e\u0440\u0438\u044f<\/td> <td>5<\/td> <td>2<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>3.50<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u041b\u0438\u0442\u0435\u0440\u0430\u0442\u0443\u0440\u0430<\/td> <td>4<\/td> <td>4<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.00<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u041c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430<\/td> <td>4<\/td> <td>5<\/td> <td>3<\/td> <td>5<\/td> <td>4<\/td> <td>4<\/td> <td>5<\/td> <td>2<\/td> <td>3<\/td> <td>5<\/td> <td>4.00<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u041e\u0431\u0449\u0435\u0441\u0442\u0432\u043e\u0437\u043d\u0430\u043d\u0438\u0435<\/td> <td>4<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.00<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0420\u0443\u0441\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a<\/td> <td>5<\/td> <td>5<\/td> <td>4<\/td> <td>5<\/td> <td>5<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.80<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0422\u0430\u0442\u0430\u0440\u0441\u043a\u0430\u044f \u043b\u0438\u0442\u0435\u0440\u0430\u0442\u0443\u0440\u0430<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0422\u0430\u0442\u0430\u0440\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a<\/td> <td>4<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.00<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0424\u0438\u0437\u0438\u043a\u0430<\/td> <td>3<\/td> <td>4<\/td> <td>5<\/td> <td>4<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.00<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0424\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043a\u0443\u043b\u044c\u0442\u0443\u0440\u0430<\/td> <td>5<\/td> <td>4<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.50<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr> <td>\u0425\u0438\u043c\u0438\u044f<\/td> <td>4<\/td> <td>5<\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td><\/td> <td>4.50<\/td><td><\/td> <td><\/td> <td><\/td> <\/tr> <tr><td colspan=\"11\"><strong>\u0418\u0422\u041e\u0413\u041e<\/strong><\/td><td>3.88<\/td> <td>0<\/td> <td>0<\/td> <td>-<\/td><\/tr> <\/tbody> <\/table>"}
我尝试用这个JSON数据创建一个NSDictionary并从那里获取NSData:
NSData *htmlData = [[NSData alloc] init];
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil]; // responseData contains API response
htmlData = [jsonDictionary objectForKey:@"html"];
TFHpple *parser = [TFHpple hppleWithHTMLData:htmlData];
NSArray *tableRows = [mainParser searchWithXPathQuery:@"//tr/td"]
但是我得到了像here这样的错误。但我无法使用那里的解决方案,因为我没有表格的URL,我只有它的HTML代码。我怎么解析它?
答案 0 :(得分:0)
您正在从NSString
[jsonDictionary objectForKey:@"html"];
个对象
无论你如何在这种情况下实际声明接收变量NSData
,它将被分配方法实际返回的内容。在这种情况下,这是一个NSString
。
通常,建议首先在id类型的变量中接收数据,然后使用isKindOfClass
检查其类,然后从那里开始。
NSData *htmlData;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
id nextElement = [jsonDictionary objectForKey:@"html"];
if (id isKindOfClass:[NSString class]) {
htmlData = [(NSString*)id dataUsingEncoding:NSUTF8StringEncoding];
} else {
// Deal with the error or cover other classes accordingly
}
TFHpple *parser = [TFHpple hppleWithHTMLData:htmlData];
NSArray *tableRows = [mainParser searchWithXPathQuery:@"//tr/td"]