我有一些 JSON 数据。这是字典数组 SwiftyJson 数组称为 jsonObj["Customer"] 并且看起来像:
[{
"kode_customer": 1,
"nama_customer": "Logam Jaya, UD",
"alamat_customer": "Rajawali No 95",
"kodepos": 60176,
"kode_provinsi": 11,
"gps_lat": -7.233834999999999,
"gps_long": 112.72964666666667
}, {
"kode_customer": 2,
"nama_customer": "Terang, TK",
"alamat_customer": "Raya Dukuh Kupang 100",
"kodepos": 60225,
"kode_provinsi": 11,
"gps_lat": -7.285430000000001,
"gps_long": 112.71538333333335
}, {
"kode_customer": 3,
"nama_customer": "Sinar Family",
"alamat_customer": "By Pass Jomin No 295",
"kodepos": 41374,
"kode_provinsi": 9,
"gps_lat": -6.4220273,
"gps_long": 107.4748978
}, {
"kode_customer": 4,
"nama_customer": "Lancar Laksana, TB",
"alamat_customer": "Jendral Sudirman No 69",
"kodepos": 41374,
"kode_provinsi": 9,
"gps_lat": -6.4220273,
"gps_long": 107.4748978
}]
现在我想用这种方式过滤数据
let filterdData = self.jsonObj["Customer"].filter({(JSON) -> Bool in
return self.jsonObj["Customer"]["kodepos"] < 6000
})
我现在想看两个结果。但这不起作用,我认为是因为
之间缺少“索引”self.jsonObj["Customer"] and ["kodepos"]
或者换个说法
print (self.jsonObj["Customer"]["kodepos"])
...我只想查看 kodepos 的所有值
如何过滤 SwiftyJson 中的数据。
最佳答案
如果您使用的是 SwiftyJSON,请尝试使用以下代码片段。它工作得很好。
let jsonObj = some JSON
let jobj = jsonObj.arrayValue
if !jobj.isEmpty {
let j = jobj.filter({ (json) -> Bool in
return json["country"].stringValue == "US"; })
print ("filterdData: \(j)")
}
注意:jobj必须是.arrayValue,否则不生效。
关于arrays - 过滤 SwiftyJson 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38828717/