无法快速解析json

问题描述 投票:0回答:1

我有一个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。但是我在总变量中得到的是零。如何获取细分总价值?

json swift alamofire
1个回答
0
投票

您会漏掉字典的内键路径。希望这就是你想要做的]

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

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.