2017-05-05 137 views
-2

我使用此代碼在購物車中訂購產品。我正在嘗試獲取生成訂單的訂單ID。我不太確定發生了什麼,因爲「結果」是AnyHashable值,ID是我需要的值。代碼運行時,它總是打印零?解析JSON返回無

Moltin.sharedInstance().cart.order(withParameters: orderParameters, success: { (responceDictionary) -> Void in 
    print(responceDictionary) 

    let orderID1 = responceDictionary!["result.id"] as? String 
    let orderID = orderID1 

    print(orderID) 

我試圖分析這些數據:

Optional([AnyHashable("status"): 1, AnyHashable("result"): { 
"bill_to" =  { 
    data =   { 
     "address_1" = "123 Sunny Street"; 
     "address_2" = Sunnycreek; 
     city = Sunnyvale; 
     company = ""; 
     country =    { 
      data =     { 
       code = US; 
       name = "United States"; 
      }; 
      value = "United States"; 
     }; 
     county = California; 
     "created_at" = "2017-04-22 19:34:48"; 
     customer =    { 
      data =     { 
       "created_at" = "2017-04-22 19:34:48"; 
       email = "[email protected]"; 
       "first_name" = Jon; 
       group = "<null>"; 
       id = 1498789797559271902; 
       "last_name" = Doe; 
       order = "<null>"; 
       password = "<null>"; 
       "updated_at" = "2017-04-22 19:34:48"; 
      }; 
      value = Jon; 
     }; 
     "first_name" = Jon; 
     id = 1498789800293958111; 
     instructions = ""; 
     "last_name" = Doe; 
     order = "<null>"; 
     phone = 6507123124; 
     postcode = CA94040; 
     "save_as" = ""; 
     "updated_at" = "2017-04-22 19:34:48"; 
    }; 
    value = ""; 
}; 
"created_at" = "2017-05-05 11:11:38"; 
currency =  { 
    data =   { 
     code = GBP; 
     "created_at" = "<null>"; 
     "decimal_point" = "."; 
     default = 1; 
     enabled = 1; 
     "exchange_rate" = 0; 
     format = "\U00a3{price}"; 
     id = 1490649105943233368; 
     modifier = "+0"; 
     rounding = "<null>"; 
     "thousand_point" = ","; 
     title = "British Pound"; 
     "updated_at" = "<null>"; 
    }; 
    value = "British Pound"; 
}; 
customer =  { 
    data =   { 
     "created_at" = "2017-04-22 19:34:48"; 
     email = "[email protected]"; 
     "first_name" = Jon; 
     group = "<null>"; 
     id = 1498789797559271902; 
     "last_name" = Doe; 
     order = "<null>"; 
     password = "<null>"; 
     "updated_at" = "2017-04-22 19:34:48"; 
    }; 
    value = Jon; 
}; 
gateway =  { 
    data =   { 
     description = "<null>"; 
     enabled = 1; 
     name = Dummy; 
     slug = dummy; 
    }; 
    value = Dummy; 
}; 
id = 1507958626625520537; 
order = "<null>"; 
"ship_to" =  { 
    data =   { 
     "address_1" = "123 Sunny Street"; 
     "address_2" = Sunnycreek; 
     city = Sunnyvale; 
     company = ""; 
     country =    { 
      data =     { 
       code = US; 
       name = "United States"; 
      }; 
      value = "United States"; 
     }; 
     county = California; 
     "created_at" = "2017-04-22 19:34:48"; 
     customer =    { 
      data =     { 
       "created_at" = "2017-04-22 19:34:48"; 
       email = "[email protected]"; 
       "first_name" = Jon; 
       group = "<null>"; 
       id = 1498789797559271902; 
       "last_name" = Doe; 
       order = "<null>"; 
       password = "<null>"; 
       "updated_at" = "2017-04-22 19:34:48"; 
      }; 
      value = Jon; 
     }; 
     "first_name" = Jon; 
     id = 1498789800293958111; 
     instructions = ""; 
     "last_name" = Doe; 
     order = "<null>"; 
     phone = 6507123124; 
     postcode = CA94040; 
     "save_as" = ""; 
     "updated_at" = "2017-04-22 19:34:48"; 
    }; 
    value = ""; 
}; 
shipping =  { 
    data =   { 
     company = "Royal Mail"; 
     "created_at" = "2017-04-20 16:14:00"; 
     description = ""; 
     id = 1497239180449678231; 
     order = "<null>"; 
     price =    { 
      data =     { 
       formatted =      { 
        tax = "\U00a30.80"; 
        "with_tax" = "\U00a34.79"; 
        "without_tax" = "\U00a33.99"; 
       }; 
       raw =      { 
        tax = "0.798"; 
        "with_tax" = "4.788"; 
        "without_tax" = "3.99"; 
       }; 
       rounded =      { 
        tax = "0.8"; 
        "with_tax" = "4.79"; 
        "without_tax" = "3.99"; 
       }; 
      }; 
      value = "\U00a34.79"; 
     }; 
     "price_max" = 0; 
     "price_min" = 0; 
     slug = "free-shipping"; 
     status =    { 
      data =     { 
       key = 1; 
       value = Live; 
      }; 
      value = Live; 
     }; 
     "tax_band" =    { 
      data =     { 
       "created_at" = "<null>"; 
       description = "<null>"; 
       id = 1490649106035507967; 
       rate = 20; 
       title = Default; 
       "updated_at" = "<null>"; 
      }; 
      value = Default; 
     }; 
     title = "Standard Shipping"; 
     "updated_at" = "2017-04-20 16:14:21"; 
     "weight_max" = 0; 
     "weight_min" = 0; 
    }; 
    value = "Standard Shipping"; 
}; 
status =  { 
    data =   { 
     key = unpaid; 
     value = Unpaid; 
    }; 
    value = Unpaid; 
}; 
totals =  { 
    "shipping_price" =   { 
     formatted = "\U00a34.79"; 
     raw = "4.788"; 
     rounded = "4.79"; 
    }; 
    subtotal =   { 
     formatted = "\U00a32,900.00"; 
     raw = 2900; 
     rounded = 2900; 
    }; 
    tax =   { 
     formatted = "\U00a3580.00"; 
     raw = 580; 
     rounded = 580; 
    }; 
    total =   { 
     formatted = "\U00a33,484.79"; 
     raw = "3484.79"; 
     rounded = "3484.79"; 
    }; 
}; 
"updated_at" = "2017-05-05 11:11:38"; 
+0

key的值' id'似乎是一個'Int' – vadian

+0

@vadian你能指點我嗎,以及如何糾正這種情況? – Owen26

+0

嘗試讓letID1 = responceDictionary![「result.id」]爲? Int' – SeanLintern88

回答

0

你不可錯過的關鍵路徑的關鍵訂閱和id是一個int:

if let resultDictionary = responceDictionary["result"] as? [String:Any], 
    let orderID = resultDictionary["id"] as? Int { 
     let orderIDString = "\(orderID)" 
     print(orderIDString) 
} 
+0

我需要在語句中使用用戶orderIDString:'if let oid = orderIDString {}'。我得到這個錯誤:條件綁定的初始化器必須有可選類型,而不是'字符串' – Owen26

+0

'orderIDString'是一個非可選字符串。 – vadian

+0

Xcode不喜歡它分配給另一個變量的方式嗎?有沒有另一種方法來將其加入字符串? – Owen26