xcode并从NSDictionary中提取数据值(objectForKey& valueForKey)

时间:2012-12-04 22:57:40

标签: xcode json ios5

我对数据类型NSDictionary感到困惑,因为它与JSON结构有关并需要帮助。这是我的JSON输出:

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

由于

1 个答案:

答案 0 :(得分:1)

这只是一个数组。您可以像这样访问其内容。

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]);