过滤来自mediawiki api ios的数据 [英] filter data from mediawiki api ios

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

问题描述

我使用了action = query& prop = revisions& rvprop = content& titles =%@& format = json& redirectsapi来获取有关Anil_Ambani的详细信息。作为回应,我得到以下字典

I used the "action=query&prop=revisions&rvprop=content&titles=%@&format=json&redirects" api for getting the details about Anil_Ambani. In response i got the following dictionary

<i> query =     {
    normalized =         (
                    {
            from = "Anil_Ambani";
            to = "Anil Ambani";
        }
    );
    pages =         {
        1222313 =             {
            ns = 0;
            pageid = 1222313;
            revisions =                 (
                                    {
                    "*" = "{{BLP sources|date=June 2012}}\n{{Infobox person\n| name                     = Anil Ambani \n| image            =AnilAmbani.jpg\n| image_size       = \n| caption              = Ambani in 2009\n| birth_date       = {{Birth date and age|1959|6|4|df=y}}\n| birth_place      = [[Mumbai]], [[Maharashtra]], [[India]]\n| nationality      = Indian\n| occupation       = Chairman of [[Anil Dhirubhai Ambani Group]] \n| 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> \n| residence        = Mumbai, Maharashtra, India\n| alma_mater       = [[Warwick Business School]]<br />[[Wharton School of the University of Pennsylvania|The Wharton School]]\n| parents          = [[Dhirubhai Ambani]]<br>Kokilaben Ambani\n| brother          = [[Mukesh Ambani]]\n| spouse           = [[Tina Ambani]]\n|

的值>这是一个 NSString 的值是

<i>$3 = 0x0755bb50 {{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</i>

现在我想过滤字符串以获取名称,birth_date等我试过

now i want to filter the string to get the name, birth_date, etc. I tried

<i>NSArray *arr=[htmlSrc componentsSeparatedByString:@"|"]
for (int i=2; i<mutArr.count; i++)
{
 NSArray *DictKeyValueArray=[str componentsSeparatedByString:@"="];
 [mainDict setObject:[DictKeyValueArray objectAtIndex:1] forKey:[DictKeyValueArray    objectAtIndex:0]];
}

但是它在birth_date之后崩溃了。因为有|出生日期和年龄后。所以我不会在birth_date之后获得任何键值对。

But it is crashing after birth_date. Because there is "|" after birth date and age. So I won't get any key value pair after birth_date.

请建议我使用其他方法来过滤字符串。

Please suggest me some other way to filter the string.

推荐答案

您获得的答案是字典形式。然后,您正在检索键*的值,它基本上是一个字符串。但是,该字符串包含 HTML值。这个HTML主要是在我们的应用程序中显示 UIWebView 中的内容。


现在,您的问题是获取特定键的值,即name,birth_date等:-

我们得到的HTML字符串不确定,因为当你改变输入的单词(搜索的单词)时它会被改变,这里,输入单词是Anil Ambani,但它可以是任何东西,所以响应所以HTML值。

所以,我认为没有任何明确的方法来获取特定密钥的值。

The response you are getting is in dictionary form. You are then retrieving the value of key "*" which is basically a string. But, that string contains HTML value. This HTML is mainly to show content in UIWebView in our app.

Now, for your question to get value of particular key i.e name, birth_date, etc.:-
The HTML string that we are getting is not definite because it will be changed as you change the input word (searched word), Here, input word is Anil Ambani, but it could be anything and so the response and so the HTML value.
So, I don't think there would be any definite way to get the values of particular key.

更新: -


要获得两个子字符串之间的子字符串,您可以使用: -

Updated:-
To get a substring between two substring, you can use this:-

-(NSString*)useScannerToGetSubstring:(NSString*)strYourString StartString:(NSString*)startString  EndString:(NSString*)endString
{
    NSString * strTest = strYourString;
    NSMutableArray *substrings = [NSMutableArray new];
    NSScanner *scanner = [NSScanner scannerWithString:strTest];
    [scanner scanUpToString:startString intoString:nil]; 
    while(![scanner isAtEnd]) {
        NSString *substring = nil;
        [scanner scanString:startString intoString:nil]; 
        if([scanner scanUpToString:endString intoString:&substring]) {
            [substrings addObject:substring];
        }
        [scanner scanUpToString:startString intoString:nil];
    }
    if ([substrings count]>0) 
    {
        return [substrings objectAtIndex:0];
    }
    return nil;
}

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

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