接收到的 json 数据是 531 字节的形式,并在 swift 4.1 和 Xcode 9.2 中给出 curruptedData 错误
Posted
技术标签:
【中文标题】接收到的 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 【发布时间】:2018-04-25 09:39:42 【问题描述】:我正在尝试使用 Swift 4.1 和 Xcode 9.2 从服务器获取 JSON 形式的响应。但是 JSONDecoder 接收到的 JSON 数据正在打印 531 bytes 并给出错误
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840"JSON 文本没有以数组或对象以及允许未设置片段的选项。" 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 中无法运行。我被困在这里很长时间了。 *** 上的所有其他问题都表明我的 JSON 文件无效,但它在其他地方工作。
注意。如果有任何问题,请随时编辑我的问题。
我已经解决了所有类似的问题,但没有一个在我的场景中起作用。
【问题讨论】:
在catch let error
范围内,执行let dataString = String.init(data:data, encoding: .utf8)
并检查它是否真的有效。这可能是由于您没有在请求中放入某些标头,例如专门要求 JSON。
@Larme 出现错误:
404 Page Not Found
找不到您要查找的页面。检查地址栏以确保您的 URL 拼写正确。如果一切都失败了,您可以通过以下链接访问我们的主页。
访问主页跨度> 这很正常。然后检查 URL。 @Larme 正如我之前提到的,该网址在 Android Project 和 Postman 中有效。 邮递员可能会在标题中添加自己接受 application/json 等内容... 【参考方案1】:解决了问题,只需要在 URL 中添加 POST 请求
foodUrl.httpMethod = "POST"
【讨论】:
以上是关于接收到的 json 数据是 531 字节的形式,并在 swift 4.1 和 Xcode 9.2 中给出 curruptedData 错误的主要内容,如果未能解决你的问题,请参考以下文章