检查Jsonobject for Int

时间:2018-01-10 00:14:35

标签: ios arrays json swift string

我有一个问题,我得到一个Json。数据出现在字典中。

这是一个示例json:

Receivedtext: {
  "x": "pricef",
  "b": "usd",
  "ds": [
    "tpr",
    "avgp",
    "mcap",
    "ppc7D",
    "ppc12h",
    "ppc4h",
    "ppc24h"
  ],
  "data": [
    [
      "ADA/USD",
      "0.819",
      "21.23B",
      "6.09",
      "-5.45",
      "-5.36",
      "-10"
    ],
    [
      "AVT/USD",
      "5.968",
      "35.81M",
      "24.33",
      "-4.51",
      "-3.3",
      "6.65"
    ],
    [
      "BAT/USD",
      "0.946",
      "unknown",
      null,
      null,
      null,
      null
    ],  [
      "FUN/USD",
      "0.000",
      "0.00",
      0,
      0,
      0,
      0
    ] ]
}

通常json应该都是String。我可以处理nill / null但我不知道如何处理它的Int / Double。

如果是Int / Double我想用“unknown”替换值。

到目前为止,这是我的代码:

struct JsonMaintableWebsocket {
    let tpr: String?
    let avgp: String?
    let mcap: String?
    let ppc7D: String?
    let ppc12h: String?
    let ppc4h: String?
    let ppc24h: String?


    init(json: [String?]) {
        self.tpr = json[0]
        self.avgp = json[1]
        self.mcap = json[2]
        self.ppc7D = json[3]
        self.ppc12h = json[4]
        self.ppc4h = json[5]
        self.ppc24h = json[6]
    }

    static func fetchJsonWebsocketMaintable(json: Data) -> [JsonMaintableWebsocket] {
        var jsonWebsocket: [JsonMaintableWebsocket] = []
        do {
            let jsonData = try JSONSerialization.jsonObject(with: json, options: []) as? [String: Any?]
            if let data = jsonData!["data"] as? [[String?]] {
                for d in data { 
                    jsonWebsocket.append(JsonMaintableWebsocket(json: d))
                }
            }
        }
        catch let error{
            print(error.localizedDescription)
        }
        return jsonWebsocket
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

您可以处理此问题的一种方法是在数组上map并检查存储的值类型,并采取相应措施。您可以更改此部分:

if let data = jsonData!["data"] as? [[String?]] {
  for d in data { 
    jsonWebsocket.append(JsonMaintableWebsocket(json: d))
  }
}

到此:

if let data = jsonData!["data"] as? [[Any?]] {
  for d in data { 
    let adjustedArray: [String?] = d.map({
      //First, check to see if object is nil, and return nil if so
      if $0 == nil {
        return nil

      //Check to see if value is string, and return string
      } else if let stringValue = $0 as? String {
        return stringValue

      //Otherwise return "unknown"
      } else {
        return "unknown"
      }
    })
    jsonWebsocket.append(JsonMaintableWebsocket(json: adjustedArray))
  }
}

通过添加更多选项,还可以轻松处理IntDouble作为字符串:

if let data = jsonData!["data"] as? [[Any?]] {
  for d in data { 
    let adjustedArray: [String?] = d.map({
      if $0 == nil {
        return nil
      } else if let stringValue = $0 as? String {
        return stringValue
      } else if let intValue = $0 as? Int {
        return "\(intValue)"
      } else if let doubleValue = $0 as? Double {
        return "\(doubleValue)"
      } else {
        return "unknown"
      }
    })
    jsonWebsocket.append(JsonMaintableWebsocket(json: adjustedArray))
  }
}