0

I have this (very long)json file that I need to parse

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

The problem that I am facing is that I have a lot of sub json objects I wrote this function to parse this file

-(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];
            }


        }
    }
}

This Function is giving me an exception here:

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

I am new to this Thanks for your help and your patience

MikO
  • 18,243
  • 12
  • 77
  • 109
janeloulou
  • 17
  • 5
  • Could you please format your question correctly. It's too crazy to even start doing it myself. – Popeye Apr 23 '13 at 11:27
  • 3
    That's just plain ol' JSON -- nothing special. You "peel" one layer at a time, like an onion. Your problem is that "clients" is an array (of 2 elements), not a dictionary. "Pretty print" the JSON using an online formatter such a [this one](http://json.parser.online.fr/), and use NSLog with each layer, to examine what you have. (Just be aware that NSLog replaces `[]` with `()`.) – Hot Licks Apr 23 '13 at 11:27
  • @Popeye - Use an online formatter -- it's better than trying to read JSON in an SO window anyway. – Hot Licks Apr 23 '13 at 11:28
  • `objectForKey` is your issue, right? Something like `[NSArray objectForKey] : unrecognized selector sent to instance`... Reading [this](http://stackoverflow.com/questions/1062183/difference-between-objectforkey-and-valueforkey) might be useful. – esh Apr 23 '13 at 12:05
  • Depending on what data you're actually looking to extract, consider using a JSON parser class like http://superloopy.io/json-framework/api/3.0/interfaceSBJsonStreamParser.html – Wain Apr 23 '13 at 12:13
  • clients is an Array not Dictornary.... – SAMIR RATHOD Apr 23 '13 at 12:50

1 Answers1

0

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

That should probably be [client objectForKey:@"contacts"]. Notice client in the singular, not the plural.

yaakov
  • 5,552
  • 35
  • 48