0

我对数据类型 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
            }
        ],

谢谢

4

1 回答 1

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]);
于 2012-12-04T23:06:37.463 回答