我成功地從我的服務器獲取數據。並得到它後,我發送數據的功能解析;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;
}
結果是一樣的。沒有什麼改變 – meth 2012-08-17 11:24:23
也許嘗試不使用jsonData作爲inbetween步驟? id jsonObjects = [NSJSONSerialization JSONObjectWithData:s .... – 2012-08-17 11:47:08