How to parse HTML JSON String

I am troubling to parse JSON from Html String, Please suggest me better way to parse.

<html><head></head><body>{"data":{"type":"success","message":"Thanks for order with XYZ."}}</body></html> 

This Html string response from stringByEvaluatingJavaScriptFromString API.

I want to parse "type" and "message" from above string. I am not getting expected result from rangeOfString API.

Thanks in advances.

1 answer

  • answered 2018-01-14 11:37 Stefanf

    Assuming you need this as a dictionary in Obj-C:

    NSString * html = @"<html><head></head><body>{\"data\":{\"type\":\"success\",\"message\":\"Thanks for order with XYZ.\"}}</body></html>";
    NSData * data = [html dataUsingEncoding:NSUTF8StringEncoding];
    NSAttributedString * attributedString = [[NSAttributedString alloc] initWithHTML:data documentAttributes:nil];
    NSData * jsonData = [attributedString.string dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary * result = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
    NSLog(@"Output %@",result);