gpt4 book ai didi

ios - Swift 4 可解码 : The given data was not valid JSON

转载 作者:IT王子 更新时间:2023-10-29 05:44:41 26 4
gpt4 key购买 nike

我正在尝试向我的本地服务器写入一个 POST 请求,这是我的功能:

@IBAction func postButtonAction(_ sender: UIButton) {

guard let url = URL(string:"http://localhost:443/api/message") else {return}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
print("POSTED")

let date : Date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateTime = dateFormatter.string(from: date)

let newPost = MessageForPost(message: "Hi", timestamp: dateTime, latitude: "1.1", longitude: "2.2")
let newData = DataForPost(message: newPost)
let newPackage = PackageForPost(data: newData)

do {
let jsonBody = try JSONEncoder().encode(newPackage)
request.httpBody = jsonBody
print("jsonBody:",jsonBody)
let jsonBodyString = String(data: jsonBody, encoding: .utf8)
print("JSON String : ", jsonBodyString!)
} catch let err {
print("jsonBody Error: ",err)
}

let session = URLSession.shared
let task = session.dataTask(with: request){ (data,response,err) in

guard let data = data else {return}

do{
let sendPost = try JSONDecoder().decode(PackageForPost.self, from: data)
print("DATA:\(data)")
}catch let err{

print("Session Error: ",err)
}
}
task.resume()
}

这些是这里使用的结构:

struct PackageForPost:Encodable, Decodable{
let data: DataForPost
}

struct DataForPost:Encodable, Decodable{
let message: MessageForPost
}

struct MessageForPost:Codable {
let message: String
let timestamp: String
let latitude: String
let longitude: String
}

它能够打印

JSON String :  {"data":{"message":{"message":"Hi","timestamp":"2017-10-18 00:50:13","latitude":"1.1","longitude":"2.2"}}}

但它一直显示这个错误:

Session Error: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))

为什么它不是有效的 JSON???

这是我的服务器的 API POST 请求文档:

POST
/api/message

{
data: {
message: {

message: “Hey, a new message!”,
timestamp: 2017-09-10 10:22:33,
latitude: 62.233589156441724,
longitude: 25.735066461654696
}
}
}

我已经进行了相当多的谷歌搜索,但我被困在这里很长时间了!任何帮助表示赞赏!

最佳答案

很简单...我遇到了同样的问题。

看你的结构是解码和编码时间戳,纬度和经度,因为字符串和 JSON 是 Double 或 Floating 类型。

struct MessageForPost:Codable {
let message: String
let timestamp: String
let latitude: String
let longitude: String
}

这里是 JSON

{
data: {
message: {
message: "Hey, a new message!",
timestamp: 2017-09-10 10:22:33,
latitude: 62.233589156441724,
longitude: 25.735066461654696
}
}
}

....我最后修复了它并且......它起作用了......

已编辑使用相同的数据类型...

关于ios - Swift 4 可解码 : The given data was not valid JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46805378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com