Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义嵌套类的JSONDecoder在swift 4中不起作用_Ios_Swift_Jsondecoder - Fatal编程技术网

Ios 自定义嵌套类的JSONDecoder在swift 4中不起作用

Ios 自定义嵌套类的JSONDecoder在swift 4中不起作用,ios,swift,jsondecoder,Ios,Swift,Jsondecoder,我使用以下代码将数据从服务器解码为json。但当它解析为json时。它抛出以下错误 无法读取数据,因为它的格式不正确 struct ExpoDecode: Codable { var success: Bool? var count: Int? var type: String? var results: [Expo]? enum CodingKeys: String, CodingKey { case success = "Success" case count = "C

我使用以下代码将数据从服务器解码为json。但当它解析为json时。它抛出以下错误

无法读取数据,因为它的格式不正确

struct ExpoDecode: Codable {

var success: Bool?
var count: Int?
var type: String?
var results: [Expo]?

enum CodingKeys: String, CodingKey {

    case success = "Success"
    case count = "Count"
    case type = "Type"
    case results = "Results"
}
}

struct ExpoAsset: Codable {

var assetId: String?
var asseturl: String?

enum CodingKeys: String, CodingKey {

    case assetId = "ExpoAssetId"
    case asseturl = "AsstetUrl"
}
}

struct Expo: Codable {

var id: String?
var name: String?
var location: String?
var timing: String?

var expoAssets: [ExpoAsset]?

enum CodingKeys: String, CodingKey {

    case id = "Expoid"
    case name = "Expotitle"
    case location = "Location"
    case ticketCost = "Ticketcost"
    case expoAssets = "ExpoAssets"
}

init(from decoder: Decoder) throws {

    let values = try decoder.container(keyedBy: CodingKeys.self)

    id = try values.decode(String.self, forKey: .id)
    name = try values.decode(String.self, forKey: .name)
    location = try values.decode(String.self, forKey: .location)
    ticketCost = try values.decode(String.self, forKey: .ticketCost)
    discount = try values.decode(String.self, forKey: .discount)
    startTime = try values.decode(String.self, forKey: .startTime)
    endTime = try values.decode(String.self, forKey: .endTime)

    expoAssets = try values.decode([ExpoAsset].self, forKey: .expoAssets)

}
}
解码部分是

let expoResult = try decoder.decode(ExpoDecode.self, from: data!)

请帮助识别问题

使用try/catch处理程序处理解码引发的错误,并将其记录到控制台。如果错误是由于解码json造成的,那么您可以看到解码过程中的问题到底在哪里

do {
  let expoResult = try decoder.decode(ExpoDecode.self, from: data!)
} catch {
  print("Error occurred: \(error)") // notice this line which will help you hunt for error
}

请指定“不工作”。您是否收到编译器/运行时错误?如果是这样,请编辑您的问题并包含错误。好的,那么您是否尝试像上面所示那样从解码器记录错误。是的。错误已损坏(Swift.DecodingError.Context(codingPath:[],debugDescription:“给定的数据不是有效的JSON.”,underlineError:Optional(error Domain=nscocaerorDomain Code=3840)“JSON文本没有以数组或对象开头,并且不允许设置片段的选项。”UserInfo={NSDebugDescription=JSON文本未以数组或对象开头,并且未设置允许片段的选项。})