【问题标题】:How to display JSON having HTML data?如何显示具有 HTML 数据的 JSON?
【发布时间】:2017-10-28 18:54:35
【问题描述】:

我在 JSON 中有 HTML 标记和实体,我不知道如何在我的应用程序中显示它。我尝试删除新行,尝试解码 html 实体,但没有任何效果。

以下是我拥有的 JSON 示例:

{"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 :- parul@india-shine.in<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: pal@ishine.in<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":"kumar178dilip@gmail.com","job_status":""}]}

【问题讨论】:

  • 实际上不可能使用上面的 json,因为没有解析器能够解析这个.. 例如br / 任何解析器如何识别它是一个中断标记
  • @Sharpkits 是的!我有太多这样的 JSON 响应,我想我以某种方式管理其中的许多,通过编码和解码 html 实体,现在应该做什么?我可以在其中使用异常处理吗?
  • 您能否提供更多关于您能够解析多少的详细信息?以及您希望显示的输出显示是什么

标签: html ios objective-c json


【解决方案1】:

其实有两种方法

方法 1

代码

@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

用法

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

方法 2

查看NSString category for XMLEntities。有一些方法可以解码 XML 实体(包括所有 HTML 字符引用)、编码 XML 实体、剥离标签以及从字符串中删除换行符和空格:

- (NSString *)stringByStrippingTags;
- (NSString *)stringByDecodingXMLEntities; // Including all HTML character references
- (NSString *)stringByEncodingXMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;

【讨论】:

    【解决方案2】:

    您可以使用 UIWebView 并像这样显示它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-20
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      相关资源
      最近更新 更多