0

我正在使用 Bing Search API 并且能够成功解析 xml 但不是 JSON。下面是解析 xml 和 JSON 的代码,当我 nslog 输出 JSON 时它显示“null”我不知道如何从这里。

 -(void)searchBing:(NSString *)text{
    //NSString *query1 = @"San Francisco Baseball";
    NSString *query = [NSString stringWithFormat: @"'%@'",text];

    //NSString *query = query1;
    NSString *format = @"atom";
    NSString *market = @"'en-us'";
    //NSInteger top = 2;

    NSMutableString *fullURL = [NSMutableString stringWithCapacity:256];
    [fullURL appendString:URL];
    [fullURL appendFormat:@"Web?$format=%@", format];
    [fullURL appendFormat:@"&Query=%@",
     [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [fullURL appendFormat:@"&Market=%@",
     [market stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   // [fullURL appendFormat:@"&$top=%d", top];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:
                                   [self getRequest:fullURL] delegate:self];
    if (connection)
    {
        NSLog(@"Connection established");
    }
    else
    {
        NSLog(@"Connection failed");
    }
}

下面是解析xml(成功)和JSON(不成功)的地方

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now
    // convert to JSON
    NSLog(@"Finished loading: Received %d bytes of data",[self.responseData length]);
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData: self.responseData];
    [parser setDelegate: self];
    [parser parse];

    NSLog(@"XMl == %@",parser);


    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:&myError];

    NSLog(@"json data = %@",res);//getting null

}

我正在使用 Base_64 编码,对于所有查看者来说,查询没有任何问题,因为通过 xml 解析器获取正确的信息。但我想要JSON中的响应。

结构样本

{
   "SearchResponse":{
      "Version":"2.2",
      "Query":{
         "SearchTerms":"testign"
      },
      "Spell":{
         "Total":1,
         "Results":[
            {
               "Value":"testing"
            }
         ]
      },
      "Web":{
         "Total":5100,
         "Offset":0,
         "Results":[
            {
               "Title":"Testign part 2 - Tiernan OTooles Programming Blog",
               "Description":"If this works, it means nothing really, but i have managed to build a .TEXT blog posting app. could be handy if i move my main blog to .TEXT, which i am thinking about..",
               "Url":"http:\/\/weblogs.asp.net\/tiernanotoole\/archive\/2004\/09\/24\/233830.aspx",
               "DisplayUrl":"http:\/\/weblogs.asp.net\/tiernanotoole\/archive\/2004\/09\/24\/233830.aspx",
               "DateTime":"2008-10-21T05:08:05Z"
            }
         ]
      }
   }
}
4

1 回答 1

0

从你的帖子:

 NSXMLParser *parser = [[NSXMLParser alloc] initWithData: self.responseData];
.
.
.
 NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:&myError];

看起来您正在尝试使用与 XML 和 JSON 相同的数据。你不能那样做。从服务器返回的数据是一种形式或另一种形式。两者都不会。

于 2013-10-22T12:35:48.170 回答