IOS / xcode调试崩溃处理json feed

时间:2015-01-09 13:14:37

标签: ios debugging

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"

笔记是桌子的名字,但除此之外我无能为力。

感谢任何建议。

1 个答案:

答案 0 :(得分:1)

您的数据源格式为:

{
    "notes": [
        {
            "row": {
                "shnote": <...>,
                "lnote": <...>
            }
        },
        {
            "row": {
                "shnote": <...>,
                "lnote": <...>
            }
        },
        <...>
    ]
}

因此,获取每一行内容的步骤应为:

  1. 读取notes属性
  2. 的值
  3. 遍历每个row
  4. 读取row属性
  5. 的值
  6. 阅读shnotelnote属性
  7. 您错过了第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);
    }