0

私は Bing Search API を使用しており、xml を正常に解析できますが、JSON は解析できません。以下は、xml と JSON の両方を解析するコードです。JSON の出力を nslog すると、「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