我从API响应接收值,如下所示

{
    "first_name" = "active EQ 'true'";
}

我需要在另一个API中将该值作为查询参数发送。但是当发送请求时,我看到URL没有被编码。我希望active EQ 'true'应该被URL编码为active%20EQ%20%27true%27
我试过了。我看到反斜杠被添加为转义符,但没有按预期编码。
let k = "active EQ 'true'"
let j = k.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print("j is \(j)")

我期望输出为addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) and it resulted in "active%20EQ%20\'true\',但实际输出为active%20EQ%20%27true%27

最佳答案

先从字符串中省略“'”。检查以下代码:

let k = "active EQ 'true' "
let i = k.replacingOccurrences(of: "'", with: "")
let j = i.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print("j is \(j!)")

10-07 19:54
查看更多