代码之家  ›  专栏  ›  技术社区  ›  Kitcc

从多维JSON数组检索值

  •  0
  • Kitcc  · 技术社区  · 10 年前

    嗨,我正在尝试从以下JSON文件(dataArray)中获取主题名称值,我已经通过NSJSONSERALISATION运行了该文件:

    -0:  {
    id: "55edc05848177ec741daf79e"
    firstName: "Brad"
    rating: 4.2
    lessons: 5
    text: "Lessons, they're yours take it"
    -subjects: [4]
         -0:  {
         name: "Indonesian"
         pricePerHour: "500000"
         }
         -1:  {
         name: "Diving"
         pricePerHour: "700000"
         }
    

    但如果使用我对其他元素使用的indexPath和点符号,则无法访问。我理解这是必须的,因为我必须访问两个数组中的NSDictionary元素,但是还没有找到正确的代码来实现这一点(尽管在这里已经做了很多研究,大多数示例都是针对更简单的情况)。 这是我一直在尝试的代码,但正如我提到的,我现在看到,这段代码并没有覆盖JSON数组中的数组。我应该如何修改它以获得值:

    // Load and display subjects
        UILabel *subjectLabel = (UILabel *)[cell viewWithTag:103];
        NSString * subject1String = [dataArray[indexPath.row] valueForKeyPath: @"subjects.0.name"];
    
        subjectLabel.text = [NSString stringWithFormat:@"%@", subject1String];
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Simran    10 年前

    我认为这是无效的Json。打印从服务器端收到的响应。为了得到 姓名: 您需要添加循环。