IOS 新手在这里调试时遇到问题。
我正在尝试处理 json 提要,但下面的代码在
- (void)viewDidLoad {
[super viewDidLoad];
shnote = @"shnote”;
lnote = @"lnote”;
myObject = [[NSMutableArray alloc] init];
self.title=@"Challenges";
NSData *jsonSource = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.~~/webservice.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
//BREAKS HERE
NSString *shnote_data = [dataDict objectForKey:@"shnote”];
//ABOVE LINE HIGHLIGHTED IN GREEN AT BREAKPOINT
NSString *lnote_data = [dataDict objectForKey:@"lnote”];
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
shnote_data, shnote,lnote_data, lnote,nil];
[myObject addObject:dictionary];
}
/*
*/
}
控制台中突出显示的行是
dataDict = (NSDictionary *const)@"notes"
笔记是表的名称,但除此之外我一无所知。
任何建议将不胜感激。
数据源的格式为:
{
"notes": [
{
"row": {
"shnote": <...>,
"lnote": <...>
}
},
{
"row": {
"shnote": <...>,
"lnote": <...>
}
},
<...>
]
}
因此,获取每行内容的步骤应为:
- 读取
notes
属性的值 - 循环访问每个
row
- 读取
row
属性的值 - 读取
shnote
和lnote
属性
您错过了步骤 1、2 和 3。在代码中:
NSURL *url = [NSURL URLWithString:@"http://www.~~/webservice.php"];
NSData *jsonSource = [NSData dataWithContentsOfURL:url];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainers error:nil];
NSDictionary *notes = jsonObject[@"notes"];
for(NSDictionary *note in notes) {
NSDictionary *row = note[@"row"];
NSString *shnote = row[@"shnote"];
NSString *lnote = row[@"lnote"];
NSLog(@"%@, %@", shnote, lnote);
}