3

I am new to NSJSONSerialization. Here's my browser output:

{
  "appointment": [
    {
      "w1": "Mallya Hospital",
      "w2": "Nagarjuna Hospital",
      "w3": "Mallige     Hospital",
      "w4": "Mallya Hospital",
      "t1": [
        "08:00 - 08:25",
        "08:25 - 08:50",
        "08:50 - 09:15",
        "09:15 - 09:40"
      ],
      "t2": [
        "14:00 - 14:05",
        "14:05 - 14:10",
        "14:10 - 14:15",
        "14:15 - 14:20",
        "14:20 - 14:25",
        "14:25 - 14:30",
        "14:30 - 14:35",
        "14:35 - 14:40",
        "14:40 - 14:45",
        "14:45 - 14:50",
        "14:50 - 14:55",
        "14:55 - 15:00",
        "15:00 - 15:05",
        "15:05 - 15:10",
        "15:10 - 15:15",
        "15:15 - 15:20",
        "15:20 - 15:25",
        "15:25 - 15:30",
        "15:30 - 15:35",
        "15:35 - 15:40",
        "15:40 - 15:45",
        "15:45 - 15:50",
        "15:50 - 15:55"
      ],
      "t3": [
        "14:00 - 14:35",
        "14:35 - 15:10",
        "15:10 - 15:45"
      ],
      "t4": [
        "16:30 - 17:15",
        "17:15 - 18:00"
      ],
      "tp1": "25",
      "tp2": "5",
      "tp3": "35",
      "tp4": "45",
      "ts1": "8:00 - 10:00",
      "ts2": "14:00 - 16:00",
      "ts3": "14:00 - 16:00",
      "ts4": "16:30 - 18:30",
      "offdays": "4-6"
    }
  ]
}

I need to pass the timings into labels and hospital names into another. How can I use NSJSONSerialization to get timings given above?

Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
user1871889
  • 31
  • 1
  • 3

1 Answers1

1

First you must convert the string/file as data that can be deserialized

NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];

To visualize how your data is going to be organized in the JSON object, a quick beautifier will show that your data follows the structure Dictionary->Array (1 element)->Dictionaries->Arrays/Values

For example, to access the times specifically:

NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &e]; //Options can be simply 0

NSArray *t1 = jsonObject[@"appointment"][0][@"t1"];
John
  • 2,675
  • 2
  • 18
  • 19
  • thanks for your response .. i copied the code but iamb getting an error showing "Array Subscript not an integer " – user1871889 Dec 03 '12 at 08:47
  • here is my code --- NSURL *urlBook = [NSURL URLWithString:strBook]; NSData *dataBook = [NSData dataWithContentsOfURL:urlBook]; jsonBookDict = [NSJSONSerialization JSONObjectWithData:dataBook options:nil error:nil]; NSArray *t1 = jsonBookDict[@"appointment"][0][@"t1"]; NSArray * keys; keys = [jsonBookDict allKeys]; for (int i = 0; i <= [keys count]-1; i++) { NSArray * arrayBook=[[jsonBookDict valueForKey:[keys objectAtIndex:i]] valueForKey:@"t2"]; NSString * strBook = [arrayBook objectAtIndex:i]; – user1871889 Dec 03 '12 at 08:55
  • That means you're using something besides an integer to access an array. Double check your JSON objects to make sure you aren't mistaking dictionaries for arrays – John Dec 03 '12 at 14:36