【问题标题】:How to Parse URLs from JSON string?如何从 JSON 字符串解析 URL?
【发布时间】:2014-02-26 06:03:07
【问题描述】:

我目前正在使用 AFNetworking 从 Wordpress API 使用 JSON Web 服务。

这是我使用的格式:

[
{
id: 4,
title: "post1",
permalink: “http://www.example.com/test-post”,
content: “blah blah blah blah blah <iframe src="//player.vimeo.com/video/1010101?title=0&amp;byline=0&amp;portrait=0&amp;color=ff5546" height="638" width="850" allowfullscreen="" frameborder="0"></iframe> more text blah blah <a href="http://example.com/wp-content/uploads/2014/01/testpic.png"><img class="aligncenter size-full wp-image-1180" alt="testpic" src="http://example.co/wp-content/uploads/2014/01/testpic.png" width="850" height="611" /></a>",
excerpt: " blah blah blah",
date: "2014-02-24 23:32:19",
author: "admin",
categories: 
[
"Uncategorized"
],
tags: [ ]
},
{
id: 1,
title: "Hello world!",
permalink: “http://www.example.com/test-post”,
content: "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!",
excerpt: "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!",
date: "2014-02-24 22:48:12",
author: "admin",
categories: 
[
"Uncategorized"
],
tags: [ ]
}
]

我的消费方式如下:

-(void)makeWordpressRequest
{
    NSURL *URL = [NSURL URLWithString:@"http://example.com/feed/json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.wordpress = responseObject;
        [self.tableView reloadData];


        NSLog(@"%@", responseObject);


    } failure:nil];
    [operation start];
}

现在这是我的问题.. 我可以成功解析并显示 JSON 的“内容”字符串,但我还没有弄清楚如何从该字符串中删除视频的“iframe”和“a href”到显示它。

有没有直接嵌入它们?或者我可以以编程方式将它们从 JSON 字符串中拆分出来,然后以另一种方式嵌入它们?我目前正在使用 UITextView 来显示@“内容”。

编辑:

不仅可以删除 HTML,还可以在可能的情况下正确嵌入它..

感谢您的帮助

【问题讨论】:

    标签: ios objective-c json parsing


    【解决方案1】:

    您的 API 不应该在 JSON 中为您提供 XML/HTML。*

    理想情况下,您应该使用 API 来解决此问题,因为在 JSON 中解析 XML/HTML 会为错误带来新的可能性。例如,可能存在字符编码问题、转义问题等。这是糟糕的 API 设计。您似乎正在使用 Wordpress,因此您可能需要在此处发布一个单独的问题,了解如何修改 Wordpress JSON API 以获取您在应用中需要的 URL。

    如果您真的无法修改 API,则必须从 JSON 中取出字符串,然后通过 NSScanner 或 NSXMLParser 运行它以尝试挑选出您想要的部分。我会从 NSScanner 开始,因为它更简单,而且我猜你的 API 不能保证你会得到有效的 XML。 Here's some sample code to get your started.

    最后,稍微偏离主题:您可能希望使用 AFNetworking 类 AFRequestOperationManager 而不是构建操作并立即启动它们。它可以使您的代码更简单、更高效。现在不急,只是想鼓励你看看。


    *(可能;这条规则也有例外。如果您应该在 UIWebView 中显示内容,也许可以接受。)

    【讨论】:

    • 是的,我要检查是否有不包含 HTML 的 API。我的最终目标是将它们正确嵌入到文本中(图像和视频)。是 UITextView适合那个或任何其他建议?
    【解决方案2】:

    //下载并#import "RegexKitLite.h" myregex 是 RegexKitLite 的引用。

    NSString *HTMLTags = @"<[^>]*>"; //regex to remove any html tag
    
    NSString *htmlString = @"<html>bla bla bla bla bla bla bla bla</html>";
    NSString *stringWithoutHTML = [hstmString stringByReplacingOccurrencesOfRegex:myregex withString:@""];
    

    希望对你有帮助...

    【讨论】:

    • contentToHTMLString 是私有方法,应用已被拒绝使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多