1

我正在尝试解析从 Objective C 中的 Wordpress 博客获取的 JSON 数据,但它返回了我不需要的杂乱/垃圾。例如,主页上有 10 个帖子,我可以正确访问帖子的标题和日期,但内容(主要帖子区域)包括广告注入代码等垃圾。

有什么方法可以在没有混乱/垃圾的情况下获取数据?

这是我的代码:

NSURL *blogURL = [NSURL URLWithString:@"http://ioshacker.com/?json=1"];

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];


self.posts = [dataDictionary objectForKey:@"posts"];



NSString *title = [[self.posts objectAtIndex:0]valueForKey:@"title_plain"];
NSString *date = [[self.posts objectAtIndex:0]valueForKey:@"date"];

NSString *content = [[self.posts objectAtIndex:0]valueForKey:@"content"];


NSLog(@"%@", title);
NSLog(@"%@", date);
NSLog(@"%@", content);

这是输出:

2015-05-24 17:11:31.282 iOSHacker[576:39153] Developing Nuclear Weapons? You might be in breach of iTunes terms and conditions
2015-05-24 17:11:31.283 iOSHacker[576:39153] 2015-05-23 23:50:55
2015-05-24 17:11:31.283 iOSHacker[576:39153] 
<!--Ad Injection mfunc mode ad include code--><!--mfunc include_once('/home/thinkios/public_html/wp-content/plugins/ad-injection/adshow.php') --><!--/mfunc-->
<p><a href="http://i2.wp.com/ioshacker.com/wp-content/uploads/2015/05/Nuclear-Weapons-and-Nuclear-Reactors.jpg"><img class="aligncenter size-full wp-image-10887" src="http://i2.wp.com/ioshacker.com/wp-content/uploads/2015/05/Nuclear-Weapons-and-Nuclear-Reactors.jpg?resize=592%2C394" alt="Nuclear-Weapons-and-Nuclear-Reactors" data-recalc-dims="1" /></a></p>
<p>If you are developing Nuclear Weapons in your secret laboratory and using iTunes or related Apple doing while doing it then you are in breach of the iTunes End User License Agreement, no seriously. Apple has made it quite clear in iTunes terms and conditions that you cannot use iTunes or its sub products such as the App Store for development, design, manufacture and production of the nuclear weapons. The company goes one step further by also prohibiting you from developing missiles, chemical and biological weapons using the said software.</p>
<!--Ad Injection mfunc mode ad code--><!--mfunc adshow_display_ad_file_v2(array('ad_random_1.txt'), array(100), array('align' => 'float left', 'clear' => '', 'margin_top' => '0', 'margin_bottom' => '1', 'padding_top' => '1', 'padding_bottom' => '0'), array(), array()) -->
<div style='float:left;margin-top:0px;margin-bottom:1px;padding-top:1px;padding-bottom:0px;margin-right:5px;'><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- iOSHacker above post (top) -->
<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client=""
     data-ad-slot=""></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div><!--/mfunc-->
<p>While we understand the company may have only added this ridiculous clause in iTunes EULA because the government requires them to cover all the basis in their user agreements, but whoever added it must have a wild imagination. Now I am not a scientist who understands Nuclear Physics but I do know that iTunes cannot be used in any way in the production of Nuclear Weapons.</p>
<p>What&#8217;s even funnier is that most users who agree to Apple&#8217;s terms and conditions never read them. Now just think about what else you have agreed to when you clicked the &#8216;I Agree&#8217; button the last time.</p>
<p>Here&#8217;s the part of <a href="http://www.apple.com/legal/internet-services/itunes/appstore/dev/stdeula/" target="_blank">iTunes EULA</a> clause that talks about Nuclear weapons.</p>
<blockquote><p>&#8220;You also agree that you will not use these products for any purposes prohibited by United States law, including, without limitation, the development, design, manufacture or production of nuclear, missiles, or chemical or biological weapons.&#8221;</p></blockquote>
4

1 回答 1

1

你得到的混乱是 JSON 的一部分,没有问题。它是“内容”项目的一部分。

特别是以下内容:dataDictionary[@"posts"][0][@"content"]

它是帖子的 HTML 内容。

如果有部分内容是您不想要的,则必须编写代码来删除这些部分。

于 2015-05-24T14:14:57.533 回答