2012-08-17 28 views
1

我成功地從我的服務器獲取數據。並得到它後,我發送數據的功能解析;NSJSONSerialization解析錯誤既不字典也不排列

- (void)readIn:(NSMutableData *)s { 
    NSLog(@"Reading in the following:"); 
    NSString * prints = [[NSString alloc] initWithData:s encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", prints); 

    NSError *error = nil; 
    NSData *jsonData = [[NSData alloc] initWithData:s]; 

    if (jsonData) { 

     id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 

     if ([jsonObjects isKindOfClass: [NSArray class]]) 
      NSLog(@"yes we got an Array"); 
     else if ([jsonObjects isKindOfClass: [NSDictionary class]]) 
      NSLog(@"yes we got an dictionary"); 
     else 
       NSLog(@"neither array nor dictionary!"); 



     if (error) { 
      NSLog(@"error is %@", [error localizedDescription]); 
      return; 
     } 

     NSArray *keys = [jsonObjects allKeys]; 
     for (NSString *key in keys) { 
      NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]); 
     } 

    } else { 

     // Handle Error 
    } 
    } 

現在我在控制檯上打印的是:

2012-08-17 13:59:57.667 TaraftarlikOyunu[1157:c07] Reading in the following: 
2012-08-17 13:59:57.667 TaraftarlikOyunu[1157:c07] {"uID":"5878341","tm":"fb","hh":122,"pt":75,"coin":500,"ll":1,"qlevel":1,"coect":true,"potWeekly":{"pts":75,"intval":604800000},"acent":{"chamunt":0},"mes":[]} 
2012-08-17 13:59:57.668 TaraftarlikOyunu[1157:c07] neither array nor dictionary! 
2012-08-17 13:59:57.670 TaraftarlikOyunu[1157:c07] error is The operation couldn’t be completed. (Cocoa error 3840.) 

似乎合法JSON對象給我。我在哪裏做錯了?

我使用nsstream從服務器獲取數據;這裏是我的代碼來獲取數據:

case NSStreamEventHasBytesAvailable: { 
      if(stream == inputStream) { 
       NSLog(@"inputStream is ready."); 

       uint8_t buf[1024]; 
       unsigned int len = 0; 

       len = [inputStream read:buf maxLength:1024]; 
      NSLog(@"length %i", len); 
       if(len > 0) { 

        NSMutableData* data=[[NSMutableData alloc] initWithLength:0]; 
        [data appendBytes: (const void *)buf length:len]; 
        [self readIn:data]; 

       } 
      } 
      break; 
     } 

回答

0

問題是,我得到的json字符串在末尾帶有空終止符號,當我嘗試反序列化它時,它無法轉換爲NSDictionary或NSArray。在代碼上做一點改動會使所有事情都完美無缺。真正的代碼應該是像其他的一個

case NSStreamEventHasBytesAvailable: { 
     if(stream == inputStream) { 

      NSLog(@"inputStream is ready."); 

      uint8_t buf[1024]; 
      unsigned int len = 0; 

      len = [inputStream read:buf maxLength:1024]; 
      NSLog(@"length %i", len); 
      if(len > 0) { 

       datum =[[NSMutableData alloc] initWithLength:0]; 

       [datum appendBytes: (const void *)buf length:len-1]; 




       NSDictionary * jsondict = [NSJSONSerialization JSONObjectWithData:datum options:NSUTF8StringEncoding error:nil]; 

       NSLog(@"is valid json object %d",[NSJSONSerialization isValidJSONObject:jsondict]); 


       [self readIn:datum]; 
       } 
      } 
      else { 
       NSLog(@"no buffer!"); 
      } 
      break; 
      } 


    default: { 
     NSLog(@"Stream is sending an Event: %i", event); 

     break; 
    } 
} 

唯一的區別就是我把最後一個字節,併成爲有效的JSON字典。 感謝對我的問題感興趣的人。

0

嘗試明確設置了一個JSONObjects是一個數組:

NSError *myError = nil; 

NSArray *jsonObjects= [NSJSONSerialization JSONObjectWithData:responseData ptions:NSJSONReadingMutableLeaves error:&myError]; 


for (NSDictionary * dict in jsonObjects) { 
    NSLog(@"Some data %@", [dict objectForKey:@"field"]); 
    //replace this to access a valid field 
    } 
+0

結果是一樣的。沒有什麼改變 – meth 2012-08-17 11:24:23

+0

也許嘗試不使用jsonData作爲inbetween步驟? id jsonObjects = [NSJSONSerialization JSONObjectWithData:s .... – 2012-08-17 11:47:08

0

的失敗原因是原始數據可能有一定的「\」字符在其中引用'''字符,如果你搜索了「可可錯誤3840」,你會得到一個提示,我建議你做的是打印出原始數據,一次一個字符(它的ascii所以沒有必要對於UTF)並驗證它。

char * ptr = [s bytes]; f或(int i = 0; i < [s length]; ++ i)NSLog(@「%c」,* ptr ++);

+0

返回的是零,不是任何對象 - 這就是爲什麼錯誤是有效的。 – 2012-08-17 11:55:14

+0

是的,它在開始時獲得空間,在數據結束時獲得'\ 0'。 – meth 2012-08-17 12:12:04

+0

NSString有一個方便的方法來「修剪」字符串的任何一端的空白 - 顯然你可以使用它。您也可以清除空字符串。但是,您的問題可能更深入,您需要首先仔細檢查如何獲取該JSON。祝你好運! – 2012-08-17 12:55:22

0

JSON不接受比標籤,換頁,回車和換行一個JSON文檔中的其他任何控制字符,所以你的代碼運行完美,究竟用不讀什麼是應該做的任何東西。

那麼這個nul角色從哪裏來?您的代碼讀取數據錯誤,或者服務器錯誤。在我看來,問題在於服務器。通過丟棄最後一個字符來解決你的問題是不好的 - 如果服務器是固定的,你會丟掉最後的大括號。我會聯繫誰負責服務器並解決問題。

相關問題