错误消息提示找不到编码密钥json

时间:2019-08-26 10:19:30

标签: ios json swift decoding codable

我正在尝试解码API调用,但不断收到错误消息:

  

keyNotFound(CodingKeys(stringValue:“ instruction”,intValue:nil),   Swift.DecodingError.Context(codingPath:[],debugDescription:“否   与键CodingKeys(stringValue:\“ instruction \”,   intValue:nil)(\“指令\”)。“,底层错误:nil))

This is the json data
"resourceSets": [
        {
            "resources": [
                {
                    "routeLegs": [
                        {
                            "itineraryItems": [
                                {
                                    "compassDirection": "south",
                                    "details": [
                                        {
                                            "compassDegrees": 182,
                                            "endPathIndices": [
                                                1
                                            ],
                                            "maneuverType": 

"DepartStart",
                                            "mode": "Driving",
                                            "names": [
                                                "Infinite Loop"
                                            ],
                                            "roadType": "Street",
                                            "startPathIndices": [
                                                0
                                            ]
                                        }
                                    ],
                                    "iconType": "Auto",
                                    "instruction": {
                                        "formattedText": "<Action>Depart</Action> <RoadName>Infinite Loop</RoadName> toward <Toward>Mariani Ave</Toward>",
                                        "maneuverType": "DepartStart",
                                        "text": "Depart Infinite Loop toward Mariani Ave"
                                    },

Here is my data structs and coding keys
struct TruckRoute: Codable {
    let resourceSets: [ResourceSet]

    enum CodingKeys: String, CodingKey {
        case resourceSets
    }
}

// MARK: - ResourceSet
struct ResourceSet: Codable {
    let resources: [Resource]
}

// MARK: - Resource
struct Resource: Codable {
    let routeLegs: [RouteLeg]

    enum CodingKeys: String, CodingKey {
        case routeLegs
    }
}

// MARK: - RouteLeg
struct RouteLeg: Codable {
    let itineraryItems: [ItineraryItem]

    enum CodingKeys: String, CodingKey {
        case itineraryItems
    }
}

// MARK: - ItineraryItem
struct ItineraryItem: Codable {
    let instruction: Instruction

    private enum CodingKeys: String, CodingKey {
        case instruction
    }
}

// MARK: - Instruction
struct Instruction: Codable {
    let formattedText, maneuverType, text: String

    private enum CodingKeys: String, CodingKey {
        case formattedText, maneuverType, text
    }
}

https://dev.virtualearth.net/REST/v1/Routes/Truck?wp.0=37.33233141,%20-122.0312186&wp.1=33.1956369,%20-117.3784666&key=AhLvBEtcGrKMDAIHHOhIhDm-aMM_dvGqstttp3wLfs8-J2A0UVBgN5bkzvaiZhj8

0 个答案:

没有答案