Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

Categories

0 votes
520 views
in Technique[技术] by (31.9m points)

json - How to exclude the result of the specified keyword

I would like to specify some keywords from the json result and exclude the result. (For example, in the image, I want to exclude two results of McDonald's of name.) I searched variously, but I do not know how to do it.

Thanks!

json.results

for i in 0...totalHitCount - 1{
    if json["results"][i]["geometry"]["location"]["lat"] != "" &&
    json["results"][i]["geometry"]["location"]["lng"] != "" &&
    json["results"][i]["name"] != "" {
        let extractedExpr = ShopData(lat: json["results"][i]["geometry"]["location"]["lat"].double,lng:json["results"][i]["geometry"]["location"]["lng"].double,name:json["results"][i]["name"].string)
        print(json["results"][i]["name"].string)
        let shopData = extractedExpr
        self.shopDataArray.append(shopData)
        print(self.shopDataArray.debugDescription)
    }

    else{
        print("")
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

To filter some names create a blacklist, the contents can be just parts of the names for example

let blackList = ["McDonald", "Kentucky"]

Then filter for

  • lat and long exists

  • name exists and is not empty

  • blacklist does not contain name

    let results = json["results"].arrayValue
    for result in results {
        let location = result["geometry"]["location"]
        if let lat = location["lat"].double,
            let lng = location["lng"].double,
            let name = result["name"].string, !name.isEmpty,
            !blackList.contains(where: { name.contains($0) }) {
    
            let extractedExpr = ShopData(lat: lat, lng: lng, name: name)
            print(name)
            let shopData = extractedExpr
            self.shopDataArray.append(shopData)
            print(self.shopDataArray.debugDescription)
    
        } else {
            print(result)
        }
    }
    

The code is a bit swiftier and you are strongly encouraged to drop SwiftyJSON in favor of Codable


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
...