2016-08-08 71 views
2

我有一些JSON數據。這是字典 數組的數組SwiftyJson被稱爲jsonObj [ 「客戶」],看起來像:過濾器SwiftyJson數據

[{ 
    "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 
}] 

現在我想FO在這樣

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中的數據?

回答

0

一種方法是使用.arrayValue您SwiftyJSON對象來創建過濾器

let customers = self.jsonObj["Customer"].arrayValue 
let filterdData = customers.filter(){ 
    item = $0 
    return item["Customer"]["kodepos"].intValue < 6000  
} 

參考標部分中的SwiftyJSON庫https://github.com/SwiftyJSON/SwiftyJSON#subscript

0

與下面的代碼片段,如果嘗試你正在使用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,否則將無法正常工作。