无法在swift中分析json

无法在swift中分析json,json,swift,alamofire,Json,Swift,Alamofire,我有一个json响应,如下所示 { "payment_methods" = ( { code = checkmo; title = "Check / Money order"; }, { code = "paypal_express"; title = "PayPal Express Checkout";

我有一个json响应,如下所示

{
    "payment_methods" =     (
                {
            code = checkmo;
            title = "Check / Money order";
        },
                {
            code = "paypal_express";
            title = "PayPal Express Checkout";
        },
                {
            code = banktransfer;
            title = "Bank Transfer Payment";
        }
    );
    totals =     {
        "base_currency_code" = USD;
        "base_discount_amount" = "-258.38";
        "base_grand_total" = "2725.37";
        "base_shipping_amount" = 400;
        "base_shipping_discount_amount" = 0;
        "base_shipping_incl_tax" = 400;
        "base_shipping_tax_amount" = 0;
        "base_subtotal" = "2583.75";
        "base_subtotal_with_discount" = "2325.37";
        "base_tax_amount" = 0;
        "coupon_code" = ADAMAS10;
        "discount_amount" = "-258.38";
        "grand_total" = "2725.37";
        items =         (
                        {
                "base_discount_amount" = "134.2";
                "base_price" = 1342;
                "base_price_incl_tax" = 1342;
                "base_row_total" = 1342;
                "base_row_total_incl_tax" = 1342;
                "base_tax_amount" = 0;
                "discount_amount" = "134.2";
                "discount_percent" = 10;
                "item_id" = 3292;
                name = BWBCA14KWGVC015;
                options = "[{\"value\":\"18K White Gold\",\"label\":\"Metal\"},{\"value\":\"2\",\"label\":\"size\"}]";
                price = 1342;
                "price_incl_tax" = 1342;
                qty = 1;
                "row_total" = 1342;
                "row_total_incl_tax" = 1342;
                "row_total_with_discount" = 0;
                "tax_amount" = 0;
                "tax_percent" = 0;
                "weee_tax_applied" = "[]";
                "weee_tax_applied_amount" = 0;
            },
                        {
                "base_discount_amount" = "124.18";
                "base_price" = "1241.75";
                "base_price_incl_tax" = "1241.75";
                "base_row_total" = "1241.75";
                "base_row_total_incl_tax" = "1241.75";
                "base_tax_amount" = 0;
                "discount_amount" = "124.18";
                "discount_percent" = 10;
                "item_id" = 3342;
                name = BWBCA14KWGCV008;
                options = "[{\"value\":\"18K White Gold\",\"label\":\"Metal Type\"},{\"value\":\"1.75\",\"label\":\"Ring Size\"}]";
                price = "1241.75";
                "price_incl_tax" = "1241.75";
                qty = 1;
                "row_total" = "1241.75";
                "row_total_incl_tax" = "1241.75";
                "row_total_with_discount" = 0;
                "tax_amount" = 0;
                "tax_percent" = 0;
                "weee_tax_applied" = "[]";
                "weee_tax_applied_amount" = 0;
            }
        );
        "items_qty" = 2;
        "quote_currency_code" = USD;
        "shipping_amount" = 400;
        "shipping_discount_amount" = 0;
        "shipping_incl_tax" = 400;
        "shipping_tax_amount" = 0;
        subtotal = "2583.75";
        "subtotal_incl_tax" = "2583.75";
        "subtotal_with_discount" = "2325.37";
        "tax_amount" = 0;
        "total_segments" =         (
                        {
                code = subtotal;
                title = Subtotal;
                value = "2583.75";
            },
                        {
                code = discount;
                title = "Discount (ADAMAS10)";
                value = "-258.38";
            },
                        {
                code = shipping;
                title = "Shipping & Handling (Flat Shipping Charge - Flat Shipping Charge)";
                value = 400;
            },
                        {
                area = taxes;
                code = tax;
                "extension_attributes" =                 {
                    "tax_grandtotal_details" =                     (
                    );
                };
                title = Tax;
                value = 0;
            },
                        {
                area = footer;
                code = "grand_total";
                title = "Grand Total";
                value = "2725.37";
            }
        );
        "weee_tax_applied_amount" = "<null>";
    };
}
在dict之前我得到了值,但在总变量中我得到了零。
如何获取总段值?

您没有提到字典的内部键路径。希望这就是你想要做的

if let res = json as? [String:Any] {

    if let paymentMethods = res["payment_methods"] as? [[String:Any]] {

    }

    if let totals = res["totals"] as? [String:Any] {
        if let items = totals["items"] as? [[String:Any]] {

        }
    }
}

使用此选项可获取
总段数
和所有
代码
键的值:

var codes = [String]()
if let res = json as? [String:Any] {
    if let totals = res["totals"] as? [String:Any] {
        if let totalSegments = totals["total_segments"] as? [[String:Any]] {
            for segment in totalSegments {
               if let code = segment["code"] as? String {
                   codes.append(code)
               }
            }
        }
    }
}

为什么不使用可编码?无法获得可用于生成可编码模型的可编码属性。
var codes = [String]()
if let res = json as? [String:Any] {
    if let totals = res["totals"] as? [String:Any] {
        if let totalSegments = totals["total_segments"] as? [[String:Any]] {
            for segment in totalSegments {
               if let code = segment["code"] as? String {
                   codes.append(code)
               }
            }
        }
    }
}