快速删除json响应中的空值
Posted
技术标签:
【中文标题】快速删除json响应中的空值【英文标题】:remove null values in json response swift 【发布时间】:2018-07-17 05:15:32 【问题描述】:我收到了这个错误:
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]
如何解决这个问题....
【问题讨论】:
不要将其转换为字符串使用像"\(yourVariable)"
这样的字符串插值不会导致崩溃
@Devil Decoder 可以发一下答案吗,我接受...
不要强行解包,这样可以避免更多崩溃。使用保护让/如果让。
@Sharad Chauhan 你能为我发布最佳解决方案吗?
Swift: How to remove a null value from Dictionary?的可能重复
【参考方案1】:
绑定时有很多方法可以从 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
,因为您永远无法确定不会有空或不正确的数据(您期望 Int 但值是从服务器发送的字符串)。
【讨论】:
我在等你的答复。请回复我。【参考方案2】:使用字符串插值解析值
喜欢你的回复
你想得到perton
let value = ((responce as! Array)[indexofarray] as! Dictionary<string,any>)["perton"]
let perton = "\(value ?? "")"
这将为第一个索引返回something
,对于第二个索引,它将返回字符串中的<null>
【讨论】:
以上是关于快速删除json响应中的空值的主要内容,如果未能解决你的问题,请参考以下文章