我正在尝试在我的项目中复制以下 GET 请求:
curl -XGET 'https://api2.branch.io/v1/url?url=https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt'
这是我的最终代码:
if let url = URL(string: "https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt"){
let urlRequest = URLRequest(url: url)
URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if error == nil{
do{
if let dataReceived = data, let jsonData = try JSONSerialization.jsonObject(with: dataReceived, options: .mutableLeaves) as? [String : Any]{
print(jsonData)
}
} catch let error{
print(error.localizedDescription)
}
}
}.resume()
}
我收到以下错误:
The data couldn’t be read because it isn’t in the correct format.
我尝试了其他 Stackoverflow 和 Reddit 解决方案,但似乎没有任何效果。
最佳答案
API 调用以错误的格式返回数据 key 。您收到的 key 以“$”或“~”开头,例如'$og_title', '~stage'。这就是它向您显示错误的原因。
- 您可以使用 Codable 来解析 API。首先,你需要 为您的 API 调用创建模型结构。
- 根据 Swift 语法更改变量名称。我已经更换了 '$' 与 'l' 和 '~' 与 'i'。你可以自定义代码 根据您的需要。
这是您的 API 调用
let url = URL(string: "https://api2.branch.io/v1/url?url=https://example.app.link/WgiqvsepqF&branch_key=key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt")
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "GET"
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
guard ((data) != nil), let _: URLResponse = response, error == nil else {
print("error")
return
}
if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) {
print(dataString)
do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
let model = try decoder.decode(ResponseData.self, from: data!) //Decode JSON Response Data
// Access your json data from Swift Structs e.g. model.type
print(model.data?.iurl)
} catch let parsingError {
print("Error", parsingError)
}
}
})
task.resume()
这是你的模型结构
struct ResponseData : Codable {
let data : MyData?
let type : Int?
let tags : [String]?
let campaign : String?
let feature : String?
let channel : String?
let stage : String?
enum CodingKeys: String, CodingKey {
case data = "data"
case type = "type"
case tags = "tags"
case campaign = "campaign"
case feature = "feature"
case channel = "channel"
case stage = "stage"
}
}
struct MyData : Codable {
let custom_array : [Int]?
let log_title : String?
let custom_boolean : Bool?
let custom_integer : Int?
let icreation_source : Int?
let log_description : String?
let log_image_url : String?
let istage : String?
let custom_string : String?
let ifeature : String?
let url : String?
let custom_object : Custom_object?
let iurl : String?
let ldesktop_url : String?
let itags : [String]?
let lcanonical_identifier : String?
let lone_time_use : Bool?
let iid : String?
let icampaign : String?
let ichannel : String?
enum CodingKeys: String, CodingKey {
case custom_array = "custom_array"
case log_title = "$og_title"
case custom_boolean = "custom_boolean"
case custom_integer = "custom_integer"
case icreation_source = "~creation_source"
case log_description = "$og_description"
case log_image_url = "$og_image_url"
case istage = "~stage"
case custom_string = "custom_string"
case ifeature = "~feature"
case url = "url"
case custom_object = "custom_object"
case iurl = "+url"
case ldesktop_url = "$desktop_url"
case itags = "~tags"
case lcanonical_identifier = "$canonical_identifier"
case lone_time_use = "$one_time_use"
case iid = "~id"
case icampaign = "~campaign"
case ichannel = "~channel"
}
}
struct Custom_object : Codable {
let random : String?
enum CodingKeys: String, CodingKey {
case random = "random"
}
}
关于ios - 无法读取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573660/