xcode和从NSDictionary提取数据值(objectForKey和valueForKey)
我对数据类型NSDictionary有点困惑,因为它属于JSON结构,需要帮助。以下是我的JSON输出:xcode和从NSDictionary提取数据值(objectForKey和valueForKey),xcode,json,ios5,Xcode,Json,Ios5,我对数据类型NSDictionary有点困惑,因为它属于JSON结构,需要帮助。以下是我的JSON输出: { "requestDetails": { "timeStamp":"2001-12-17T09:30:47-08:00", "transactionType":"QUERY", "action":"GET INVOICES", }, "Payload": { "event":
{
"requestDetails":
{
"timeStamp":"2001-12-17T09:30:47-08:00",
"transactionType":"QUERY",
"action":"GET INVOICES",
},
"Payload":
{
"event":
{
"sourceRecordType":"INVOICE INQUIRY",
"serviceRecordType":"INVOICE",
"ownershipType":"EXPLICIT",
},
},
"executionDetails":
{
"timeStamp":"2012-12-04T13:48:21-08:00",
"statusType": "SUCCESSFUL_TRANSACTION",
"statusCode":"0",
"DBRecordCount":"0",
"processedRecordCount":"0",
"warning":
[
{
"errorCode":"257",
"errorDescription":"Criteria specified is incorrect. Please Verify that the criteria is correct.",
"__hashCodeCalc":false
},
{ "errorCode":"60",
"errorDescription":"No results were found. Please enter new search criteria.",
"__hashCodeCalc":false
}
],
},
}
现在我的理解是,整个过程都是字典和objectForKey:@“executionDetails”将给出以下输出:
{
"timeStamp":"2012-12-04T13:48:21-08:00",
"statusType": "SUCCESSFUL_TRANSACTION",
"statusCode":"0",
"DBRecordCount":"0",
"processedRecordCount":"0",
"warning":
[
{
"errorCode":"257",
"errorDescription":"Criteria specified is incorrect. Please Verify that the criteria is correct.",
"__hashCodeCalc":false
},
{ "errorCode":"60",
"errorDescription":"No results were found. Please enter new search criteria.",
"__hashCodeCalc":false
}
],
}
如何选取[]括号内的值。我试过valueForKey和ObjectForKey。我不清楚处理结构,非常感谢您的帮助
warning":
[
{
"errorCode":"257",
"errorDescription":"Criteria specified is incorrect. Please Verify that the criteria is correct.",
"__hashCodeCalc":false
},
{ "errorCode":"60",
"errorDescription":"No results were found. Please enter new search criteria.",
"__hashCodeCalc":false
}
],
谢谢这只是一个数组。您可以像这样访问它的内容
NSDictionary *executionDetails = [json objectForKey:@"executionDetails"];
NSArray *warnings = [executionDetails objectForKey:@"warning"];
for (NSDictionary *warning in warnings) {
NSLog(@"%@", warning);
}
// To access an individual warning use: [warnings objectAtIndex:0]
您还可以使用现代Objective-C语法使其更清晰:
NSDictionary *executionDetails = json[@"executionDetails"];
NSArray *warnings = executionDetails[@"warning"];
NSLog(warnings[0]);
谢谢它工作得很好。但是,有没有一个地方可以让我去寻求帮助,以了解objectForKey和JSON输出是如何映射的呢?在这种情况下,您并不是真正在使用JSON。JSON已经反序列化,您正在使用或。否则,我建议您参考您正在使用的JSON库的文档,例如,谢谢。知道了。我将参考JSON库。