收到的 json 数据是 531 字节的形式,在 swift 4.1 和 Xcode 9.2 中给出 curruptedData 错误
Received json data is in form of 531 bytes and giving curruptedData error in swift 4.1 and Xcode 9.2
我正在尝试从服务器获取响应,格式为 JSON 和 Swift 4.1和 Xcode 9.2。但是 JSON 解码器 接收到的 JSON 数据正在打印 531 字节 并给出错误
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 文本未以数组或对象开头,并且未设置允许片段的选项。})))
我的请求任务是
let itemUrl = URL(string: mainUrl.MainUrl + "viewallstore")
let foodUrl = URLRequest(url: itemUrl!)
URLSession.shared.dataTask(with: foodUrl) { (data, response, error) in
print(data ?? "Errrrr")
if data != nil {
do {
let storeDetails = try JSONDecoder().decode(AllStores.self, from: data!)
for store in storeDetails.StoreDetails {
self.foods.append(FoodCourt(storeId: store.str_id, storeNo: store.str_number, storeName: store.str_name, storePhone: store.str_phone, storeParking: store.str_parking, storeOpenTime: store.str_open_time, storeCloseTime: store.str_close_tie, storeStartdate: store.str_open_date, storeCloseDate: store.str_close_date, storeLogoPath: store.str_logo_path, storeLogoFileName: store.str_logo_file_name, storeStatus: store.status, storeCategory: store.str_cat, createdAt: store.create_at, storeZone: store.str_zone, storeAvailability: store.str_availability, storeMulCategory: store.str_multiple_cat))
}
}catch let error {
self.toastNeck(message: "\(error)")
print(error)
DispatchQueue.main.async{
myActivityIndicator.stopAnimating()
myActivityIndicator.hidesWhenStopped = true
}
}
DispatchQueue.main.async {
self.collectionViewFoodCourt.reloadData()
}
}
}.resume()
}
我的结构是
struct AllStores: Decodable{
let StoreDetails: [StoreDetail]
}
struct StoreDetail: Decodable {
let str_id: String
let str_number: String
//let tid : String? //tid
let str_name: String
let str_phone: String
let str_parking: String
let str_open_time: String
let str_close_tie: String
let str_open_date: String
let str_close_date: String
let str_logo_path: String
let str_logo_file_name: String
let status: String
let str_cat: String
let create_at: String
let str_zone: String
let str_availability: String
let str_multiple_cat: String
enum CodingKey: String {
case str_id = "str_id"
case str_number = "str_number"
//case tid = "tid"
case str_name = "str_name"
case str_phone = "str_phone"
case str_parking = "str_parking"
case str_open_time = "str_open_time"
case str_close_tie = "str_close_tie"
case str_open_date = "str_open_date"
case str_close_date = "str_close_date"
case str_logo_path = "str_logo_path"
case str_logo_file_name = "str_logo_file_name"
case status = "status"
case str_cat = "str_cat"
case create_at = "create_at"
case str_zone = "str_zone"
case str_availability = "str_availability"
case str_multiple_cat = "str_multiple_cat"
}
}
我在服务器上的 json 文件是
{
"StoreDetails": [
{
"str_id": "1",
"str_number": "0",
"tid": "",
"str_name": "Moti mahal",
"str_des": "",
"str_phone": "9540011581",
"str_add": "",
"str_parking": "Ground Floor",
"str_short_dis": "Moti Mahal",
"str_open_time": "10:00:00",
"str_close_tie": "23:00:00",
"str_open_date": "2017-01-29",
"str_close_date": "2018-01-28",
"str_logo_path": "",
"str_logo_file_name": "Moti mahal.png",
"status": "Y",
"str_cat": "1",
"company_id": "0",
"str_lat": "0",
"str_lon": "0",
"create_at": "2017-02-11 19:45:28",
"create_by": "0",
"str_zone": "1",
"str_floor": "0",
"str_availability": "1",
"str_multiple_cat": "1"
},
{
"str_id": "2",
"str_number": "0",
"tid": "",
"str_name": "Ever Green",
"str_des": "",
"str_phone": "9953487923",
"str_add": "",
"str_parking": "Green Floor",
"str_short_dis": "Snakes",
"str_open_time": "10:00:00",
"str_close_tie": "22:00:00",
"str_open_date": "2017-02-05",
"str_close_date": "2017-12-31",
"str_logo_path": "",
"str_logo_file_name": "Ever Green.jpg",
"status": "N",
"str_cat": "1",
"company_id": "0",
"str_lat": "0",
"str_lon": "0",
"create_at": "2018-01-15 22:20:11",
"create_by": "0",
"str_zone": "1",
"str_floor": "0",
"str_availability": "1",
"str_multiple_cat": "1"
}
]
}
同一个 json 文件在 android 中工作正常,但在 iOS 中不工作。我被困在这里很长时间了。
Whosebug 上的所有其他问题都表明我的 JSON 文件无效,但它在其他地方有效。
注。如果有任何问题,请随时编辑我的问题。
我已经完成了所有类似的问题,但其中 none 个问题适用于我的场景。
解决了这个问题,只需要将 POST 请求添加到 URL
foodUrl.httpMethod = "POST"
我正在尝试从服务器获取响应,格式为 JSON 和 Swift 4.1和 Xcode 9.2。但是 JSON 解码器 接收到的 JSON 数据正在打印 531 字节 并给出错误
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 文本未以数组或对象开头,并且未设置允许片段的选项。})))
我的请求任务是
let itemUrl = URL(string: mainUrl.MainUrl + "viewallstore")
let foodUrl = URLRequest(url: itemUrl!)
URLSession.shared.dataTask(with: foodUrl) { (data, response, error) in
print(data ?? "Errrrr")
if data != nil {
do {
let storeDetails = try JSONDecoder().decode(AllStores.self, from: data!)
for store in storeDetails.StoreDetails {
self.foods.append(FoodCourt(storeId: store.str_id, storeNo: store.str_number, storeName: store.str_name, storePhone: store.str_phone, storeParking: store.str_parking, storeOpenTime: store.str_open_time, storeCloseTime: store.str_close_tie, storeStartdate: store.str_open_date, storeCloseDate: store.str_close_date, storeLogoPath: store.str_logo_path, storeLogoFileName: store.str_logo_file_name, storeStatus: store.status, storeCategory: store.str_cat, createdAt: store.create_at, storeZone: store.str_zone, storeAvailability: store.str_availability, storeMulCategory: store.str_multiple_cat))
}
}catch let error {
self.toastNeck(message: "\(error)")
print(error)
DispatchQueue.main.async{
myActivityIndicator.stopAnimating()
myActivityIndicator.hidesWhenStopped = true
}
}
DispatchQueue.main.async {
self.collectionViewFoodCourt.reloadData()
}
}
}.resume()
}
我的结构是
struct AllStores: Decodable{
let StoreDetails: [StoreDetail]
}
struct StoreDetail: Decodable {
let str_id: String
let str_number: String
//let tid : String? //tid
let str_name: String
let str_phone: String
let str_parking: String
let str_open_time: String
let str_close_tie: String
let str_open_date: String
let str_close_date: String
let str_logo_path: String
let str_logo_file_name: String
let status: String
let str_cat: String
let create_at: String
let str_zone: String
let str_availability: String
let str_multiple_cat: String
enum CodingKey: String {
case str_id = "str_id"
case str_number = "str_number"
//case tid = "tid"
case str_name = "str_name"
case str_phone = "str_phone"
case str_parking = "str_parking"
case str_open_time = "str_open_time"
case str_close_tie = "str_close_tie"
case str_open_date = "str_open_date"
case str_close_date = "str_close_date"
case str_logo_path = "str_logo_path"
case str_logo_file_name = "str_logo_file_name"
case status = "status"
case str_cat = "str_cat"
case create_at = "create_at"
case str_zone = "str_zone"
case str_availability = "str_availability"
case str_multiple_cat = "str_multiple_cat"
}
}
我在服务器上的 json 文件是
{
"StoreDetails": [
{
"str_id": "1",
"str_number": "0",
"tid": "",
"str_name": "Moti mahal",
"str_des": "",
"str_phone": "9540011581",
"str_add": "",
"str_parking": "Ground Floor",
"str_short_dis": "Moti Mahal",
"str_open_time": "10:00:00",
"str_close_tie": "23:00:00",
"str_open_date": "2017-01-29",
"str_close_date": "2018-01-28",
"str_logo_path": "",
"str_logo_file_name": "Moti mahal.png",
"status": "Y",
"str_cat": "1",
"company_id": "0",
"str_lat": "0",
"str_lon": "0",
"create_at": "2017-02-11 19:45:28",
"create_by": "0",
"str_zone": "1",
"str_floor": "0",
"str_availability": "1",
"str_multiple_cat": "1"
},
{
"str_id": "2",
"str_number": "0",
"tid": "",
"str_name": "Ever Green",
"str_des": "",
"str_phone": "9953487923",
"str_add": "",
"str_parking": "Green Floor",
"str_short_dis": "Snakes",
"str_open_time": "10:00:00",
"str_close_tie": "22:00:00",
"str_open_date": "2017-02-05",
"str_close_date": "2017-12-31",
"str_logo_path": "",
"str_logo_file_name": "Ever Green.jpg",
"status": "N",
"str_cat": "1",
"company_id": "0",
"str_lat": "0",
"str_lon": "0",
"create_at": "2018-01-15 22:20:11",
"create_by": "0",
"str_zone": "1",
"str_floor": "0",
"str_availability": "1",
"str_multiple_cat": "1"
}
]
}
同一个 json 文件在 android 中工作正常,但在 iOS 中不工作。我被困在这里很长时间了。 Whosebug 上的所有其他问题都表明我的 JSON 文件无效,但它在其他地方有效。
注。如果有任何问题,请随时编辑我的问题。
我已经完成了所有类似的问题,但其中 none 个问题适用于我的场景。
解决了这个问题,只需要将 POST 请求添加到 URL
foodUrl.httpMethod = "POST"