Question

I'm making a native iOS app of my team's project using it's API. API has three main methods: to get user's profile information, his diary and certificate table. Last method returns to me JSON data with HTML code of a table. It looks like this

{"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>"}

I've tried to create an NSDictionary with this JSON data and get NSData from there like this:

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"]

But I'm getting the same error like here. But I can't use the solution from there, because I don't have an URL to the table, I only have it's html code. How can I parse it?

Was it helpful?

Solution

You are receiving an NSString object from [jsonDictionary objectForKey:@"html"];

Regardless how you actually declared the receiving variable, NSData in this case, it will be assigned what the method actually returns. And that is an NSString in this case.

In general it is advisable to receive the data in a variable of type id first and then check its class using isKindOfClass and then go from there.

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"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top