使用JSON在目标c中从json消息中获取结果时遇到问题.Framework
问题描述:
我试图使用json.framework读取json消息。该消息是会议详细信息的嵌套集合。我的愿望是迭代思考所有会议,并创建具有从消息中读取的细节的本地会议对象。我看到获得json结果中15个会议的列表,但无法从结果中获取单个值。使用JSON在目标c中从json消息中获取结果时遇到问题.Framework
这是我的示例代码。我正在使用json消息的文件,因此我不必在此测试中涉及服务器。 json消息可以下载here。
-(void)TestParse:(NSString *)response
{
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"conference_calls" ofType:@"json"];
NSString *fileContent =[[NSString alloc]initWithContentsOfFile:filePath];
parser = [SBJsonParser new];
NSArray *results = [parser objectWithString:fileContent];
NSLog(@"Number of itmems in the results: --> %i", [results count]);
for(NSDictionary *conf in results){
//Load local objects with the values of the Conf info.
NSLog(@"This the description %@ ",[c valueForKey:"phone_number"]);
NSLog(@"Number of Items in Dic: %i",[conf count]);
NSLog(@"File contents: %@",[conf description]);
}
答
你的json的结构是一个字典数组。但每个字典只有一个叫做“conference_call”的密钥,该密钥的值是另一个字典,包含该调用的所有细节。
所以这样的事情应该工作:
for (NSDictionary* call in results) {
// get the actual data for this call
NSDictionary *callDetails = [call objectForKey:@"conference_call"];
NSLog (@"Location is %@", [callDetails objectForKey:@"location"]);
}
希望有所帮助。
我试过这个,当试图访问密钥时仍然会出现异常。 – gmoorevt 2011-05-22 14:36:14
哪个键和什么是例外? – 2011-05-22 14:48:00
我的错误现在有用,谢谢。 – gmoorevt 2011-06-22 22:04:36