删除json响应中的null值swift

时间:2018-07-17 05:15:33

标签: ios json swift parsing

我遇到此错误:

Could not cast value of type 'NSNull' (0x1114e4850) to 'NSString' (0x11065d2a8).

//These are my arrays
var snoArray:[Int] = []
var quote_idArray:[String] = []
var quote_amountArray:[String] = []
var dateArray:[String] = []
var timeArray:[String] = []
var perkilometerArray:[String] = []
var pertonArray:[String] = []

//My JSON response is like this.
["Response": {
Array =     (
            {
        date = "25-05-2018";
        perkilometer = 2;
        perton = "<null>";
        "quote_amount" = 1234;
        "quote_id" = 5b080f8aa082c;
        sno = 1;
        time = "18:58";
    },
            {
        date = "25-05-2018";
        perkilometer = 346;
        perton = "<null>";
        "quote_amount" = 230000;
        "quote_id" = 5b080ed57aa34;
        sno = 2;
        time = "18:55";
    }
);
"error_code" = 0;
message = SUCCESS;
status = SUCCESS;
}, "count": 2]

//I'm catching response like this...
if status == "SUCCESS" {
   if let array = res!["Array"] as? Array<Dictionary<String, Any>> {
      //    print(array.count)
      self.snoArray = array.map { $0["sno"]! } as! [Int]
      self.quote_idArray = array.map { $0["quote_id"]! } as! [String]
      self.quote_amountArray = array.map { $0["quote_amount"]! } as! [String]
      self.dateArray = array.map { $0["date"]! } as! [String]
      self.timeArray = array.map { $0["time"]! } as! [String]
      self.perkilometerArray = array.map { $0["perkilometer"]! } as! [String]
      //But here I'm getting null values and app crashed
      self.pertonArray = array.map { $0["perton"]! } as! [String]
   }
} 

//When I write like this, it's printing : [<null>, <null>]
let perTon = array.compactMap({ $0 ["perton"]})
 print(perTon) // Output: [<null>, <null>]

我引用了此链接:https://useyourloaf.com/blog/swift-non-nil-values-in-an-array-of-optionals/

在这里,我想从self.pertonArray中删除空值,并要打印空的self.pertonArray([,])。

如果想得到这样的答复,如何解决这个问题。

["Response": {
Array =     (
            {
        date = "25-05-2018";
        perkilometer = 2;
        perton = "something";
        "quote_amount" = 1234;
        "quote_id" = 5b080f8aa082c;
        sno = 1;
        time = "18:58";
    },
            {
        date = "25-05-2018";
        perkilometer = 346;
        perton = "<null>";
        "quote_amount" = 230000;
        "quote_id" = 5b080ed57aa34;
        sno = 2;
        time = "18:55";
    }
);
"error_code" = 0;
message = SUCCESS;
status = SUCCESS;
}, "count": 2] 

如何解决此问题。...

2 个答案:

答案 0 :(得分:3)

有很多方法可以在绑定时从JSON中删除nil。 但是这是根据您的代码之一。

if status == "SUCCESS" {
   if let array = res!["Array"] as? Array<Dictionary<String, Any>> {
      //    print(array.count)
      self.snoArray = array.map { ($0["sno"] as? Int) ?? 0 }
      self.quote_idArray = array.map { ($0["quote_id"] as? String) ?? "" }
      self.quote_amountArray = array.map { ($0["quote_amount"] as? String) ?? "" }
      self.dateArray = array.map { ($0["date"] as? String) ?? "" }
      self.timeArray = array.map { ($0["time"] as? String) ?? ""} 
      self.perkilometerArray = array.map { ($0["perkilometer"] as? String) ?? "" }

      // You get error here with null values
      // here is how it's fixed
      self.pertonArray = array.map { ($0["perton"] as? String) ?? "" }
   }
} 

在绑定json时,最好不要使用Force Unwrapping,因为您将永远无法确定不会有null或不正确的数据(您希望使用Int,但其值为从服务器发送的String)。

答案 1 :(得分:1)

使用字符串插值来解析值

喜欢您的回复

您想获得perton

let value = ((responce as! Array)[indexofarray] as! Dictionary<string,any>)["perton"]
let perton = "\(value ?? "")"

这将为第一个索引返回something,对于第二个索引,它将返回字符串

中的<null>