2012-04-19 104 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