【问题标题】:NSJSON SerializationNSJSON 序列化
【发布时间】:2012-12-03 07:40:10
【问题描述】:

我是NSJSONSerialization 的新手。 这是我的浏览器输出:

{
  "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"
    }
  ]
}

我需要将时间传递给标签,将医院名称传递给另一个。我如何使用NSJSONSerialization 来获取上面给出的时间?

【问题讨论】:

    标签: objective-c json nsjsonserialization


    【解决方案1】:

    首先你必须将字符串/文件转换为可以反序列化的数据

    NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
    

    为了可视化您的数据在 JSON 对象中的组织方式,quick beautifier 将显示您的数据遵循结构 Dictionary->Array (1 element)->Dictionaries->Arrays/Values

    例如,具体访问时间:

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

    【讨论】:

    • 感谢您的回复 .. 我复制了代码,但 iamb 收到错误消息,显示“数组下标不是整数”
    • 这是我的代码 --- NSURL *urlBook = [NSURL URLWithString:strBook]; NSData *dataBook = [NSData dataWithContentsOfURL:urlBook]; jsonBookDict = [NSJSONSerialization JSONObjectWithData:dataBook 选项:nil 错误:nil]; NSArray *t1 = jsonBookDict[@"约会"][0][@"t1"]; NSArray * 键;键 = [jsonBookDict allKeys]; for (int i = 0; i
    • 这意味着您正在使用除整数之外的其他东西来访问数组。仔细检查您的 JSON 对象,以确保您没有将字典误认为是数组
    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多