0

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

<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>

现在我想过滤字符串以获取名称、出生日期等。我试过了

<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之后我不会得到任何键值对。

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

4

1 回答 1

0

您得到的响应是字典形式的。然后,您将检索基本上是字符串的键“*”的值。但是,该字符串包含HTML value。这个 HTML 主要是为了在我们的应用中显示UIWebView中的内容。

现在,您的问题是获取特定键的值,即名称、出生日期等
:- 我们得到的 HTML 字符串不确定,因为它会随着您更改输入词(搜索词)而改变,在这里,输入词是 Anil Ambani,但它可以是任何东西,例如响应和 HTML 值。
所以,我认为没有任何确定的方法可以获取特定键的值。

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

-(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;
}
于 2013-07-25T08:19:54.170 回答