问题解释JSON结构|迅速

时间:2019-07-16 04:57:54

标签: json swift

我具有以下用于填充tableView的JSON结构

我需要安装我的Swift应用程序才能正确解释数据,但是由于某些原因,我无法使其正常工作(tableView为空)

JSON结构:

    struct Pickup_EnRoute: Codable {
        let name, sumValue: String, sumTotal: String

        enum CodingKeys: String, CodingKey {
            case name
            case sumValue = "sumValue"
            case sumValue = "sumTotal"
        }
    }

JSON的外观:

[{"name":"Jake","sumValue":"10084.14"}, 
{"name":"Jack","sumValue":"9428.08"},{"sumTotal":19512.22}]

注意sumTotal如何在JSON数组中不包含“”(引号)

2 个答案:

答案 0 :(得分:0)

尝试此代码

   class AViewController: UIViewController {
      override func viewDidLoad() {
         super.viewDidLoad()

         NotificationCenter.default.addObserver(self, selector: #selector(checkEventPushAuthorization(_:)), name: UIApplication.didBecomeActiveNotification, object: nil)

         EventPushManager.shared.isPushNotificationAuthorized { [weak self] (isAuthorized) in
             DispatchQueue.main.async {
                guard isAuthorized else {
                   self?.goSettingsApp(isUserAgreed)
                   return
                }
                ...
             }
         }

      }


      @objc
      private func checkEventPushAuthorization(_ notification: Notification) {
        EventPushManager.shared.isPushNotificationAuthorized { [weak self] (isAuthorized) in
              DispatchQueue.main.async {
                 self?.showResultAlert(didEventAlertAgreed)
              }
            }
        }
      }
   }

将sumTotal字符串更改为Double。

答案 1 :(得分:0)

在JSON中,“ sumTotal”的类型为Double,而您的“结构”将sumTotal定义为String。因此,将sumTotal:String更改为sumTotal:Double