0

I have HTML tags and entities in JSON, I don't know how to display it in my app. I tried removing new lines, I tried decoding html entities but nothing worked.

Below is an example of JSON I have:

{"Jobdesc":[{"jobid":"12281","job_title":"IOS Developer for Noida Location Job","job_desc":"<p><strong>Job Description</strong><br />

Dear Candidate <br />

I have an opening for IOS Developer for Noida Location for leading IT company. pli\ease find the JD below-<br />

JOB DESCRIPTION :-<br />

Industry :- IT <br />

Experience :- Min 1 Year<br />

Position :- IOS Developer<br />

Location :- Noida<br />

 <br />

<strong>Job Responsibility :- </strong><br />

 <br />

Design and build advanced applications for the iOS platform<br />

Collaborate with cross-functional teams to define, design, and ship new features</p>



<p>Working experience in iOS development<br />

Have published one or more iOS apps in the app store<br />

A deep familiarity with Objective-C and Cocoa Touch<br />

Experience working with iOS frameworks such as Core Data, Core      Animation, Core Graphics and Core Text<br />

 If you are interested in the above mentioned JD kindly share your resume with me and contact me on :-<br />

  <br />

 Thanks And Regards<br />

 Pal Mittal<br />

 Contact no :- 9200272001<br />

 Email ID :- [email protected]<br />

 <br />

 <br />

Functional Area: Web / Mobile Technologies <br />

Industry: IT - Software <br />

Skills: IOS ipad <br />

Other Skills: IOS Developer Iphone Developer IOS Application Developer    iphone Application Developer iphone<br />

 <br />

Recruiter details <br />

Company Name:  Employment Solution<br />

Email: [email protected]<br />

Telephone: 9200272001</p>

","job_role":"IOS Developer","job_exp":"1-4  year","job_education":"MCA","job_location":"Delhi","job_address":"Delhi","j  ob_company_name":"India Shine Employment  Solution","job_company_url":"www.india- shine.in","job_company_email":"[email protected]","job_status":""}]}
3
  • actually not possible with the above json because, no parser would be able to parse this.. for instance br / how would any parser recognize that its a break tag Commented Oct 28, 2016 at 10:46
  • @Sharpkits Yeah ! i have too many JSON response like this,thought somehow i managed many of them, through coding and decoding html entities , what should be done now ? can i use exception handling in this? Commented Oct 28, 2016 at 10:50
  • Can you please provide more details regarding how much you were able to parse? and what is the output display you wish to show Commented Oct 28, 2016 at 11:03

2 Answers 2

2

Actually there are 2 approaches

Approach 1

Code

@interface NSAttributedString (HTML)
+ (instancetype)attributedStringWithHTMLString:(NSString *)htmlString;
@end

@implementation NSAttributedString (HTML)
+ (instancetype)attributedStringWithHTMLString:(NSString *)htmlString
{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                               NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };

    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];

    return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
}

@end

Usage

NSString *cleanString = [[NSAttributedString attributedStringWithHTMLString:question.title] string];

Approach 2

Check out NSString category for XMLEntities. There's methods to decode XML entities (including all HTML character references), encode XML entities, stripping tags and removing newlines and whitespace from a string:

- (NSString *)stringByStrippingTags;
- (NSString *)stringByDecodingXMLEntities; // Including all HTML character references
- (NSString *)stringByEncodingXMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
Sign up to request clarification or add additional context in comments.

Comments

0

You can use UIWebView and show it like this:

let htmlString = //HTML String      
webView.delegate = self
webView.scalesPageToFit = true
webView.contentMode = .scaleAspectFit
webView.loadHTMLString(htmlString, baseURL: nil)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.