如何在一定数量以下映射和过滤JSON嵌套数据字典?

时间:2019-06-05 01:54:29

标签: swift

我有一个静态JSON文件,正在将其解码成功。但是,由于它嵌套了几层,因此我很难正确地存储它。当前控制台打印出来 ["Hamilton", "Chermside", "Coorparoo"]

但是,我需要它过滤并返回每个郊区中小于500000的值。这样的事情会很棒。

"Hamilton" "oneBRU": 341000, "twoBRU": 480000

"Chermside" "oneBRU": 320000, "twoBRU": 255000, "threeBRU": 435000, "twoBRH": 400000

静态JSON文件位于底部。非常感谢

var suburbsJson: [Suburb] = []

struct ResponseData: Codable {
    var suburbs: [Suburb]
}

struct Suburb : Codable {
    var _id: Int
    var name: String
    var postcode: Int
    var prices: SuburbPrices
}

struct SuburbPrices: Codable    {
    let oneBRU: Int
    let twoBRU: Int
    let threeBRU: Int
    let twoBRH: Int
    let threeBRH: Int
    let fourBRH: Int
}

func loadJson(filename fileName: String) -> [Suburb]? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(ResponseData.self, from: data)
            self.suburbsJson = jsonData.suburbs

            let suburb = suburbsJson.map { $0.name }
            print(suburb)

            // print only suburbs below 500000



            return jsonData.suburbs
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}

JSON文件

{
"suburbs": [
    {
        "_id": 1,
        "name": "Hamilton",
        "postcode": 4007,
        "prices":
            {
                "oneBRU": 341000,
                "twoBRU": 480000,
                "threeBRU": 880000,
                "twoBRH": 555000,
                "threeBRH": 945000,
                "fourBRH": 1200000
            }
    },
    {
        "_id": 2,
        "name": "Chermside",
        "postcode": 4032,
        "prices":
        {
            "oneBRU": 320000,
            "twoBRU": 255000,
            "threeBRU": 435000,
            "twoBRH": 400000,
            "threeBRH": 585000,
            "fourBRH": 860000
        }
    },
    {
        "_id": 3,
        "name": "Coorparoo",
        "postcode": 4151,
        "prices":
        {
            "oneBRU": 323000,
            "twoBRU": 359750,
            "threeBRU": 535000,
            "twoBRH": 500000,
            "threeBRH": 750000,
            "fourBRH": 970000
        }
    }
]
}

1 个答案:

答案 0 :(得分:0)

如果只需要打印值,则可以执行以下操作:

func loadJson(filename fileName: String) -> [Suburb]? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(ResponseData.self, from: data)
            self.suburbsJson = jsonData.suburbs

            // print only suburbs below 500000
            jsonData.suburbs.forEach { suburb in
                print(suburb.name)
                if suburb.prices.oneBRU < 500000 {
                    print("One BRU: \(suburb.prices.oneBRU)")
                }
                if suburb.prices.twoBRU < 500000 {
                    print("Two BRU: \(suburb.prices.twoBRU)")
                }
                if suburb.prices.threeBRU < 500000 {
                    print("Three BRU: \(suburb.prices.threeBRU)")
                }
                if suburb.prices.twoBRH < 500000 {
                    print("Two BRH: \(suburb.prices.twoBRH)")
                }
                if suburb.prices.threeBRH < 500000 {
                    print("Three BRH: \(suburb.prices.threeBRH)")
                }
                if suburb.prices.fourBRH < 500000 {
                    print("Four BRH: \(suburb.prices.fourBRH)")
                }
            }
            return jsonData.suburbs
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}