How do I parse JSON with Objective-C?

前端 未结 5 1640
暖寄归人
暖寄归人 2020-11-22 07:18

I am new to iPhone. Can anyone tell me the steps to follow to parse this data and get the activity details, first name, and last name?

{
    \"#error\": fals         


        
5条回答
  •  不要未来只要你来
    2020-11-22 08:02

    NSString* path  = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"json"];
    
    //将文件内容读取到字符串中,注意编码NSUTF8StringEncoding 防止乱码,
    NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    //将字符串写到缓冲区。
    NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *jsonError;
    id allKeys = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONWritingPrettyPrinted error:&jsonError];
    
    
    for (int i=0; i<[allKeys count]; i++) {
        NSDictionary *arrayResult = [allKeys objectAtIndex:i];
        NSLog(@"name=%@",[arrayResult objectForKey:@"storyboardName"]);
    
    }
    

    file:

     [
      {
      "ID":1,
      "idSort" : 0,
      "deleted":0,
      "storyboardName" : "MLMember",
      "dispalyTitle" : "76.360779",
      "rightLevel" : "10.010490",
      "showTabBar" : 1,
      "openWeb" : 0,
      "webUrl":""
      },
      {
      "ID":1,
      "idSort" : 0,
      "deleted":0,
      "storyboardName" : "0.00",
      "dispalyTitle" : "76.360779",
      "rightLevel" : "10.010490",
      "showTabBar" : 1,
      "openWeb" : 0,
      "webUrl":""
      }
      ]
    

提交回复
热议问题