IOS newb这里有调试问题。
我正在尝试处理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"
笔记是桌子的名字,但除此之外我无能为力。
感谢任何建议。
答案 0 :(得分:1)
您的数据源格式为:
{
"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);
}