ios - 如何在 Swift 中访问 Json Int、Double、String 中的数据?

标签 ios json swift parameters

我的 json 数据中有以下参数。

date = "2018-02-12";
"grand_total" = "82.88";
message = "Order Details Details";
status = "order_details";
"sub_total" = 74;
"tax_total" = "8.880000000000001";
time = "06:53:46";

如何为标签设置总计和小计。在 objective c 中,我们可以使用 nsstring string with format 直接访问字符串的任何值,但在 swift 中没有这样的选项。

最佳答案

swift 3

使用 if let 是最安全的方法。

if let gTotal = dict["grand_total"] as? Double {
     // gTotal had the double value
}
else if let gTotal = dict["grand_total"] as? Int {
     // gTotal had the int value
}
else {
     // not double nor int value.
}

if let subTotal = dict["sub_total"] as? Double {
    // subTotal had the double value
}
else if let subTotal = dict["sub_total"] as? Int {
    // subTotal had the int value
}
else {
     // not double nor int value.
}

if let message  = dict["message"] as? String {
    // message had the string value
}
else {
    // not string value
}

希望对您有所帮助。

关于ios - 如何在 Swift 中访问 Json Int、Double、String 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48741048/

相关文章:

java - 在java中解析json rest响应

swift - 你可以在 iOS 应用程序中使用 Package.swift 而不是使用 .xcodeproj 吗?

ios - 在 SwiftUI 的 UITextView 中禁用向上滚动

ios - 使用 Firebase ios 获取数据

ios - 具有多种颜色的 UIProgressView 上的 UILabel

ios - xCode 在打开后更改了 View 的大小(在 Storyboard 中)

ios - swiftui 3 View 栈布局,中间栈占空间

javascript - meteor 路线未注册

iphone - 故障安全 "multitasking"以恢复 iOS 游戏中的游戏状态?

java - 尝试将 JSON 字符串发送到 Android 客户端时出现 500 内部服务器错误 GAE?