서버에서 데이터를 성공적으로 가져 왔습니다. 그것을 얻은 후에 나는 파싱 할 함수에 데이터를 보낸다.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
}
}
이제 콘솔 내 인쇄 : 그것은 나에게 법적 JSON 개체를 보인다
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.)
. 내가 뭘 잘못하고 있니?
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
아마 inbetween 단계로 jsonData를 사용하지 않으시겠습니까? id jsonObjects = [NSJSONSerialization JSONObjectWithData : s .... –