Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

json - 解析Json Show Error keyNotFound Swift(Parsing Json Show Error keyNotFound Swift)

i want to Parsing a json but Xcode Show this message : keyNotFound(CodingKeys(stringValue: "Id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"Id\", intValue: nil) (\"Id\").", underlyingError: nil)) (我想解析一个json但Xcode显示此消息: keyNotFound(CodingKeys(stringValue:“ Id”,intValue:nil),Swift.DecodingError.Context(codingPath:[],debugDescription:“没有与键CodingKeys(stringValue:相关联的值) \“ Id \”,intValue:零)(\“ Id \”)。“,底层错误:零)))

JSON : (JSON:)

{
    "result": [
        {
            "Id": 5,
            "Title": "Test1",
            "EnTitle": "Story and Novel"
        },
        {
            "Id": 38,
            "Title": "Test2",
            "EnTitle": " Motivational"
        }

    ],
    "status": {
        "message": "Confirm",
        "success": true,
        "systemDateTime": "2019-11-01T12:07:05+03:30",
        "httpStatusCode": 200
    }
}

Model : (型号:)

struct Result : Decodable {
    let Id : Int
    let Title : String
    let EnTitle : String
}

class ResultInitialiser {

    let Id : Int
    let Title : String
    let EnTitle : String

    init(Id:Int, Title:String, EnTitle: String) {
        self.Id = Id
        self.Title = Title
        self.EnTitle = EnTitle
    }

}

View Controller : (视图控制器:)

var genresFetch = [Result]()
var structGenresFetch = [ResultInitialiser]()
let headers : HTTPHeaders = ["Token" : "6f8652e3-d9d9-4b34-9455-0fa32e82ec58"]

    AF.request(BASE_URL!, method: .get, headers: headers).response { (response) in
        do {
            self.genresFetch = [try JSONDecoder().decode(Result.self, from: response.data!)]

                for eachProduct in self.genresFetch {

                    let recived_Data = ResultInitialiser(Id: eachProduct.Id, Title: eachProduct.Title, EnTitle: eachProduct.EnTitle)
                    self.structGenresFetch.append(recived_Data)
                    print(eachProduct.Title)
                    DispatchQueue.main.async {
                  self.tvMainApi.reloadData()
                    }
                }
        }catch {
            print(error)
        }
    }
  ask by mehran.kmlf translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

"No value associated with key CodingKeys(stringValue: \"Id\", intValue: nil) (“没有与键CodingKeys关联的值(字符串值:\“ Id \”,整数值:无))

The root of the json contains result key only and doesn't have id or the other keys you submit with your Result struct , so You need (json的根仅包含result键,没有id或您通过Result结构提交的其他键,因此您需要)

self.genresFetch = try JSONDecoder().decode(Root.self, from: response.data!)
print(self.genresFetch.result)

struct Root : Decodable {
    let result : [Result]

}

struct Result: Decodable {
    let id: Int
    let title, enTitle: String

    enum CodingKeys: String, CodingKey {
        case id = "Id"
        case title = "Title"
        case enTitle = "EnTitle"
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...