从Wikimedia API iOS过滤数据 [英] filter data from wikimedia api ios

查看:68
本文介绍了从Wikimedia API iOS过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索 Anil_Ambani 时,我从Wikipedia api得到了令人沮丧的json响应.我用了这个api.我得到以下回应

I got the fllowing json response from wikipedia api while searching Anil_Ambani. I used this api. I got the following response

<i>$2 = 0x071882f0 {{BLP sources|date=June 2012}}
{{Infobox person
| name             = Anil Ambani 
| image            =AnilAmbani.jpg
| image_size       = 
| caption          = Ambani in 2009
| birth_date       = {{Birth date and age|1959|6|4|df=y}}
| birth_place      = [[Mumbai]], [[Maharashtra]], [[India]]
| nationality      = Indian
| occupation       = Chairman of [[Anil Dhirubhai Ambani Group]] 
| networth         = {{loss}} [[United States dollar|$]]5.2 billion (2012)<ref         name="forbes.com">[http://www.forbes.com/profile/anil-ambani/.] Forbes.com. Retrieved April 2013.</ref> 
| residence        = Mumbai, Maharashtra, India
| alma_mater       = [[Warwick Business School]]<br />[[Wharton School of the University of Pennsylvania|The Wharton School]]
| parents          = [[Dhirubhai Ambani]]<br>Kokilaben Ambani
| brother          = [[Mukesh Ambani]]
| spouse           = [[Tina Ambani]]
| children         = 2<ref>{{cite web|url=http://www.indiatoday.com/itoday/20050221/power9.html |title=India Today 2005 Power List |publisher=Indiatoday.com |date=2005-02-21 |accessdate=2010-12-31}}</ref>
|religion         = [[Hinduism]]  
|relations        = [[Mukesh Ambani]] (Brother)
|website          = {{URL|http://www.relianceadagroup.com/ada/chairman.html|Anil Ambani}} 
|footnotes        = 
}}

任何人都可以指导我如何过滤数据.由于它是一个NSString.我无法将其转换为字典.如何获取姓名,出生日期,出生地等的值.

Can anyone guide me how to filter the data. As it is a NSString. I am not able to convert it to dictionary. How to get the values for name, birthdate, birthplace, etc.

推荐答案

第一种方法:-
首先从字符串中删除空格和无关字符.
然后,以这种方式进行:-

First Approach:-
First remove White spaces and irrelevant characters from the string.
Then, proceed in this way:-

NSArray *testArray = [yourString componentsSeparatedByString:@"|"];
NSString *key = [testArray objectAtIndex:0];
NSArray *values = [testArray subarrayWithRange:NSMakeRange(1, [testArray count] - 1)];
NSDictionary *dict = [NSDictionary dictionaryWithObject:values forKey:key];


而且您可以轻松获得特定键的值,例如名称,出生地等.

第二种方法:-


And you can easily get the value of particular key like name, birthplace, etc.

Second approach:-

NSError *err = nil;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:[yourString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];


NSMutableDictionary *dict = arr[0];
    for (NSMutableDictionary *dictionary in arr) {
        // do something using dictionary
    }

这篇关于从Wikimedia API iOS过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆