首页 > 解决方案 > 无法快速解析 json

问题描述

我有一个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>";
    };
}

我需要从总段中获取值。代码是运输,价值是运输和处理(固定运费 - 固定运费。

我尝试使用以下代码解析 json

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

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

                          if let total = dict as? [[String:Any]]
                          {

                          }
                        }


                    }

我在 dict 之前得到了价值。但我在总变量中得到了零。如何获得总段值?

标签: jsonswiftalamofire

解决方案


您错过了提及字典的内部键路径。希望这是你想要做的

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]] {

        }
    }
}

推荐阅读