【问题标题】:Parse JSON using NSURLConnection使用 NSURLConnection 解析 JSON
【发布时间】:2013-10-22 10:30:17
【问题描述】:

我正在使用 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"
            }
         ]
      }
   }
}

【问题讨论】:

  • 你检查数据是否也为空?
  • 相同数据通过 xml 可见,但通过 json 不可见
  • 这很奇怪,因为如果数据很好,你必须确定一个 json 解析,你能把给你 JSON 的链接给我吗?
  • 恐怕不可能,因为您需要 api key[of bing] 才能查看。但我的问题是解析是否正确?
  • 是的,但是如果返回一个 json 并写下结构,你可以帮我检查一下?

标签: ios xml json nsurlconnection


【解决方案1】:

来自您的帖子:

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

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

【讨论】:

  • 现在我只有 NSString *json = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; NSError *myError = nil; NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:&myError]; NSLog(@"json 数据 = %@",json); NSLog(@"\n\n\n\n\n\n 数据 = %@",res);
  • 很抱歉,我无法像您那样以更好的方式呈现代码。
猜你喜欢
  • 2012-01-08
  • 1970-01-01
  • 2011-07-05
  • 2019-01-06
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多