0
我有我需要解析目標C:解析子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文件是,我有很多次的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"];
我是新來這個 感謝您的幫助和耐心
你能否正確地格式化你的問題。這太瘋狂了,甚至不能自己開始做。 – Popeye 2013-04-23 11:27:36
這只是普通的'JSON - 沒什麼特別的。你一次「剝離」一層,就像洋蔥一樣。你的問題是「客戶」是一個數組(2個元素),而不是字典。使用在線格式化程序[this one](http://json.parser.online.fr/)「JSON打印」JSON,並在每個圖層中使用NSLog來檢查您的內容。 (請注意,NSLog用'()'替換'[]') – 2013-04-23 11:27:59
@Popeye - 使用在線格式化程序 - 它總比在SO窗口中嘗試閱讀JSON要好。 – 2013-04-23 11:28:35