0

我有这个(很长的)json 文件需要解析

{
  "_status": "success",
  "_data": {
    "clients": [{
        "id": 1,
        "name": "Audi Bank",
        "contacts": [{
            "id": 1,
            "first_name": "Raymond",
            "last_name": "Audi",
            "templates": [{
                "id": 1,
                "name": "SecurityQuestionnaire",
                "title": "Security Questionnaire",
                "creation_date": "2012-08-12",
                "sections": [{
                    "id": 2,
                    "title": "Personal questions",
                    "header": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                    "footer": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                    "questions": [{
                        "id": 1,
                        "type": "MCQ_question",
                        "value": "What type of firewall would you like to install?",
                        "answers": [{
                            "id": 1,
                            "answer": "Network layer or packet filters"
                          }, {
                            "id": 2,
                            "answer": "Application-layer"
                          }, {
                            "id": 3,
                            "answer": "Proxies"
                          }, {
                            "id": 4,
                            "answer": "Network address translation"
                          }
                        ]
                      }, {
                        "id": 3,
                        "type": "numerical_question",
                        "value": "How many posts do you have approximatively in your company?"
                      }, {
                        "id": 4,
                        "type": "short_answer_question",
                        "value": "What is the name of your IT manage?"
                      }
                    ]
                  }, {
                    "id": 5,
                    "title": "Professional experience",
                    "questions": [{
                        "id": 6,
                        "type": "long_answer_question",
                        "value": "Tell us about your business needs"
                      }, {
                        "id": 8,
                        "type": "association",
                        "value": "Link the department with its number of switches",
                        "answers": {
                          "left": [{
                              "id": 6,
                              "answer": "Financial Department"
                            }, {
                              "id": 7,
                              "answer": "Marketing Department"
                            }, {
                              "id": 8,
                              "answer": "HR Department"
                            }
                          ],
                          "right": [{
                              "id": 9,
                              "answer": "over 100"
                            }, {
                              "id": 10,
                              "answer": "under 100"
                            }
                          ]
                        }
                      }, {
                        "id": 7,
                        "type": "text",
                        "title": "Remarks",
                        "value": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                      }
                    ]
                  }
                ]
              }, {
                "id": 2,
                "name": "bbbb",
                "title": "bbb",
                "creation_date": "2000-09-11",
                "sections": [{
                    "id": 2,
                    "title": "Personal questions",
                    "header": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                    "footer": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                    "questions": [{
                        "id": 1,
                        "type": "MCQ_question",
                        "value": "What type of firewall would you like to install?",
                        "answers": [{
                            "id": 1,
                            "answer": "Network layer or packet filters"
                          }, {
                            "id": 2,
                            "answer": "Application-layer"
                          }, {
                            "id": 3,
                            "answer": "Proxies"
                          }, {
                            "id": 4,
                            "answer": "Network address translation"
                          }
                        ]
                      }, {
                        "id": 3,
                        "type": "numerical_question",
                        "value": "How many posts do you have approximatively in your company?"
                      }, {
                        "id": 4,
                        "type": "short_answer_question",
                        "value": "What is the name of your IT manage?"
                      }
                    ]
                  }, {
                    "id": 5,
                    "title": "Professional experience",
                    "questions": [{
                        "id": 6,
                        "type": "long_answer_question",
                        "value": "Tell us about your business needs"
                      }, {
                        "id": 8,
                        "type": "association",
                        "value": "Link the department with its number of switches",
                        "answers": {
                          "left": [{
                              "id": 6,
                              "answer": "Financial Department"
                            }, {
                              "id": 7,
                              "answer": "Marketing Department"
                            }, {
                              "id": 8,
                              "answer": "HR Department"
                            }
                          ],
                          "right": [{
                              "id": 9,
                              "answer": "over 100"
                            }, {
                              "id": 10,
                              "answer": "under 100"
                            }
                          ]
                        }
                      }, {
                        "id": 7,
                        "type": "text",
                        "title": "Remarks",
                        "value": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }, {
        "id": 2,
        "name": "Byblos Bank",
        "contacts": [{
            "id": 2,
            "first_name": "Antoine",
            "last_name": "Atallah",
            "templates": [{
                "id": 1,
                "name": "SecurityQuestionnaire",
                "title": "Security Questionnaire",
                "creation_date": "2012-08-12",
                "sections": [{
                    "id": 2,
                    "title": "Personal questions",
                    "header": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                    "footer": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                    "questions": [{
                        "id": 1,
                        "type": "MCQ_question",
                        "value": "What type of firewall would you like to install?",
                        "answers": [{
                            "id": 1,
                            "answer": "Network layer or packet filters"
                          }, {
                            "id": 2,
                            "answer": "Application-layer"
                          }, {
                            "id": 3,
                            "answer": "Proxies"
                          }, {
                            "id": 4,
                            "answer": "Network address translation"
                          }
                        ]
                      }, {
                        "id": 3,
                        "type": "numerical_question",
                        "value": "How many posts do you have approximatively in your company?"
                      }, {
                        "id": 4,
                        "type": "short_answer_question",
                        "value": "What is the name of your IT manage?"
                      }
                    ]
                  }, {
                    "id": 5,
                    "title": "Professional experience",
                    "questions": [{
                        "id": 6,
                        "type": "long_answer_question",
                        "value": "Tell us about your business needs"
                      }, {
                        "id": 8,
                        "type": "association",
                        "value": "Link the department with its number of switches",
                        "answers": {
                          "left": [{
                              "id": 6,
                              "answer": "Financial Department"
                            }, {
                              "id": 7,
                              "answer": "Marketing Department"
                            }, {
                              "id": 8,
                              "answer": "HR Department"
                            }
                          ],
                          "right": [{
                              "id": 9,
                              "answer": "over 100"
                            }, {
                              "id": 10,
                              "answer": "under 100"
                            }
                          ]
                        }
                      }, {
                        "id": 7,
                        "type": "text",
                        "title": "Remarks",
                        "value": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}

我面临的问题是我有很多子 json 对象我写了这个函数来解析这个文件

-(void)readJson
{
    //Emplacement du json file
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"allClients?userId=2" ofType:@"json"];
    //Contenu du fichier
    NSString *jsonString = [NSString stringWithContentsOfFile:filePath];
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];



    id wordParam = [[json objectForKey:@"words"] objectForKey:@"word"];

    if ([wordParam isKindOfClass:[NSArray class]]) {
        NSDictionary *word = [(NSArray *)wordParam objectAtIndex:0];
    } else if ([wordParam isKindOfClass:[NSDictionary class]]) {
        NSDictionary *word = (NSDictionary *)wordParam;
    } else {
        NSLog(@"error. %@ is not an array or dictionary", wordParam);
    }


    //_data
    NSDictionary *items = [json objectForKey:@"_data"];
    for (NSDictionary *item in items)
    {
        NSDictionary *clients = [items objectForKey:@"clients"];
        for(NSDictionary *client in clients)
        {
        NSString * clientId=[client objectForKey:@"id"];
        NSString * name=[client objectForKey:@"name"];
        @try{
        NSDictionary *contacts=[clients objectForKey:@"contacts"];
            }
            @catch (NSException *ex) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@",ex]
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];
            }


        }
    }
}

这个函数在这里给了我一个例外:

NSDictionary *contacts=[clients objectForKey:@"contacts"];

我是新手 感谢您的帮助和耐心

4

1 回答 1

0

NSDictionary *contacts=[clients objectForKey:@"contacts"];

应该是这样[client objectForKey:@"contacts"]的。注意client单数,而不是复数。

于 2013-04-23T12:14:10.240 回答