다음 문이 올 바르거나 뭔가 빠졌습니까?NSJSONSerialization에서 반환 된 개체가 달라질 수 있습니다.
당신은이 사전 또는 배열 있는지 NSJSONSerialization
의 반환 개체를 확인해야 - 당신이
data = {"name":"joe", "age":"young"}
// NSJSONSerialization returns a dictionary
및
data = {{"name":"joe", "age":"young"},
{"name":"fred", "age":"not so young"}}
// returns an array
각 유형을 가질 수는 다른 접근 방법이있는 잘못된 것을 사용하면 끊어집니다. - 내가 스택 오버 플로우와 애플의 문서와 다른 곳을 통해 좋은 모습을했다
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers error:&error];
if ([jsonObjects isKindOfClass:[NSArray class]])
NSLog(@"yes we got an Array"); // cycle thru the array elements
else if ([jsonObjects isKindOfClass:[NSDictionary class]])
NSLog(@"yes we got an dictionary"); // cycle thru the dictionary elements
else
NSLog(@"neither array nor dictionary!");
및 위의 직접 확인을 찾을 수 없습니다
NSMutableArray *jsonObject = [json objectAtIndex:i];
// will break if json is a dictionary
그래서 당신이 그런 짓을 할 수있다 : 예를 들어 .
코드에 어떤 문제가 있는지 언급하지 않았습니다. –
문제는 사전을 다시 가져오고 배열 메서드를 사용하여 예외에 액세스 한 경우입니다. 이 문제를 해결하기 위해 반환 객체 유형을 확인해야하지만 이것이 올바른 방법임을 확인하고 싶습니다. – user1705452