请注意,您的JSON数据具有以下结构:
- 顶级值是具有单个属性(称为“课程”)的对象(字典)
- “教训”属性是一个数组
- “课程”数组中的每个元素都是一个具有几个属性的对象(包含课程的字典),其中包括“眩晕”
相应的代码是:
SBJSON *parser = [[[SBJSON alloc] init] autorelease];// 1. get the top level value as a dictionaryNSDictionary *jsonObject = [parser objectWithString:JsonData error:NULL];// 2. get the lessons object as an arrayNSArray *list = [jsonObject objectForKey:@"lessons"];// 3. iterate the array; each element is a dictionary...for (NSDictionary *lesson in list){ // 3 ...that contains a string for the key "stunde" NSString *content = [lesson objectForKey:@"stunde"];}一些观察:
在中
-objectWithString:error:
,error
参数是指向指针的指针。在这种情况下,通常使用NULL
代替nil
。如果方法返回,则 不要 传递NULL
并使用NSError
对象检查错误也是一个好主意nil
如果
jsonObject
仅用于该特定方法,则可能不需要复制它。上面的代码没有。



