2012-04-19 102 views
0

更新4月22日:JSON解析只显示前20项

问题解决了:原来,这需要API OAUTH工作......这解释了为什么它会在浏览器中正确加载(我已经登录),而不是其他应用程序,如JSON验证器或我自己的应用程序。

我想写一个类,它将在Lion上的Xcode 4.3.2中获取和解析JSON文件。如果这是一个愚蠢的问题,我只是编了几个星期才对不起!代码似乎工作正常,除了只有JSON文件中的前20个条目出现(应该有200)。任何想法,为什么这可能是?

- (void) initJSON 
{ 
NSString *urlstri = [[NSString alloc] initWithFormat:@"http://api.t.sina.com.cn/statuses/followers.json?source=?????&count=200&screen_name=%@", userName]; 
NSURL *newURL = [[NSURL alloc] initWithString:urlstri]; 
NSURLRequest *request = [NSURLRequest requestWithURL:newURL]; 

[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
     [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:NO]; 
        } 
     ];} 
- (void) fetchedData:(NSData *)responseData { 
//parse out the json data 
NSError* error; 
NSDictionary* json = [NSJSONSerialization 
         JSONObjectWithData:responseData //1 

         options:kNilOptions 
         error:&error]; 


NSArray* newestFollower = [json objectForKey:@"users"]; //2 
fnumber = 0; 
mnumber = 0; 

int arraySize = [newestFollower count]; 
NSLog(@"%i",arraySize); 

for (i=0; i<arraySize; ++i) { 
    NSDictionary* userProfile = [newestFollower objectAtIndex: i]; 
    NSString* userGender = [userProfile objectForKey:@"gender"]; 
    //NSLog(@"%@", userGender); 
    if([userGender isEqualToString:@"m"]){ 
     //NSLog (@"man"); 
     ++mnumber; 
    } 
    if([userGender isEqualToString:@"f"]){ 
     //NSLog(@"notman"); 
     ++fnumber;} 
} 

mOutput = [NSNumber numberWithInt:(mnumber)]; 
fObj = [NSString stringWithFormat:@"%i", fnumber]; 
mObj = [NSString stringWithFormat:@"%i", mnumber]; 
NSLog (@"Boys:%@", mObj); 
NSLog (@"Girls:%@", fObj); 
+0

您是否尝试过复制您的'urlStri'并粘贴到浏览器中以验证服务器是否确实返回了您想要的200个项目?当你在你的代码中的'arraySize'是否是20或200? – jonkroll 2012-04-19 15:26:58

+0

感谢您的快速回复。当我将URL复制到浏览器时,它将返回所有200个项目,但arraySize只有20个。如果我NSLog json NSDictionary,我也只能得到20个项目。 – scottmacd 2012-04-19 16:37:51

+0

您的JSON格式是否有效?请在[jsonlint.org](http://jsonlint.org)中对其进行验证并报告 – Lefteris 2012-04-19 18:01:04

回答

0

我不知道这是否会解决您的问题,但你可以尝试要求您的数据异步使用NSURLConnection,而不是同步NSDatadataWithContentsofURL方法。

这将是这个样子:

- (void) initJSON 
{ 
    NSString *urlstri = [[NSString alloc] initWithFormat:@"http://api.t.sina.com.cn/statuses/followers.json?source=??????&count=200&screen_name=%@", userName]; 
    NSURL *newURL = [[NSURL alloc] initWithString:urlstri]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:newURL]; 

    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
      [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:NO]; 
    }]; 
} 
+0

我在那里添加了代码,它不停地询问“];”在最后的大括号之前添加。我设法让它给我一个响应(尽管仍然在日志中有一条错误消息),通过添加此代码,本质上增加了它的要求并删除了错误定义(我已将更改后的代码添加到问题中以上)。但即使这样做,仍然只有20个结果... – scottmacd 2012-04-19 17:45:47

+0

糟糕,是的,我错过了一个关闭支架。 – jonkroll 2012-04-19 18:29:55

0

有可能是与编码的问题。某些字符在解析时可能会出现问题(我以前用HTMLParser库遇到过这个问题)。

首先,您可以尝试设置不同的编码器。如果这不起作用,可能会从内容字符串中替换有问题的字符。在过去的项目中,由于HTML解析器的问题,我使用了以下内容:

NSString *contents = [[NSString alloc] initWithData:data encoding:NSWindowsCP1252StringEncoding]; 

// for parsing this weird char has te be removed, else will only parse partially ... 
contents = [contents stringByReplacingOccurrencesOfString:@"Ó" withString:@"O"]; 

data = [contents dataUsingEncoding:NSWindowsCP1252StringEncoding]; 

很遗憾,我找不出更清晰的解决方案。

你应该尝试弄清楚你的内容是否有一些特殊字符无法正确解析,并在解析之前“清理”这些特殊字符的内容。

PS:从苹果对JSON序列文档

的数据必须在JSON说明书中列出的5个支持的编码之一:UTF-8,UTF-16LE,UTF-16BE,UTF- 32LE,UTF-32BE。数据可能有也可能没有BOM。用于解析的最有效的编码是UTF-8,所以如果您有编码传递给此方法的数据的选择,请使用UTF-8。

+0

刚刚改变了编码,因为你建议,仍然没有区别! – scottmacd 2012-04-19 18:51:43

+0

不要完全改变它到我拥有的。只需创建responseData的NSString并记录下来。找出该字符串是否包含分析时存在问题的字符。 – 2012-04-19 19:34:15