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
191 views
in Technique[技术] by (71.8m points)

swift - SwiftUI JSON Decode

How to json decode, null problem,I can't decode the data comes in but I can't decode it because of null values. What is the problem? I guess I need to ignore the null values, I put them as optional but still cannot decode them. print Error : Nil turn,I can see data with JsonSerieal.jsonWith Is Nil because of incoming values? What is the solution to this problem?

MYMODEL :

   struct CameraTestModel:Decodable {
        var ratingToolVideoInterviewQuestion: RatingToolVideoInterviewQuestionModel
        var ratingGroupCandidateAnswerStatus: RatingGroupCandidateAnswerStatusModel
        var ratingToolVideoInterviewQuestionCount, currentQuestionNumber: Int?
        var isLastQuestion: Bool?
        var videoInterviewEndPageTemplate, currentPage: String?
    }
    
    
    struct RatingGroupCandidateAnswerStatusModel : Decodable {
        var positionId:Int?
        var position :String?
        var candidateId:Int?
        var candidate:String?
        var isFinish:Bool?
        var candidateInterviewStatus:Int?
        var candidateStartTimeOfInterview:String?
        var candidateEndTimeOfInterview:String?
        var invitationValidtyStartTime:String?
        var invitationValidtyEndTime:String?
        var ratingGroupStageId:Int?
        var ratingGroupStage:String?
        var id:Int?
        var createdOn:String?
        var createdById:Int?
        var createdName:String?
        var lastModifiedOn:String?
        var lastModifiedById:Int?
        var lastModifiedName:String?
        var isDeleted:Bool?
        var isActive:Bool?
        var inputCustomerId:Int?
        init(from decoder: Decoder) throws {
              let container = try decoder.singleValueContainer()
              if !container.decodeNil() {
                  throw DecodingError.typeMismatch(RatingGroupCandidateAnswerStatusModel.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
              }
          }
    
          public func encode(to encoder: Encoder) throws {
              var container = encoder.singleValueContainer()
              try container.encodeNil()
          }
    }
    struct RatingToolVideoInterviewQuestionModel : Decodable{
        var ratingToolVideoQuestionId : Int?
        var ratingToolVideoQuestion:RatingToolVideoQuestionModel
        var questionTransitionText:String?
        var recordingTime : Int?
        var thinkingTime: Int?
        var order: Int?
        var ratingToolVideoInterviewId: Int?
        var ratingToolVideoInterview:String?
        var id: Int?
        var createdOn:String?
        var createdById: Int?
        var createdName:String?
        var lastModifiedOn:String?
        var lastModifiedById: Int?
        var lastModifiedName:String?
        var isDeleted:Bool?
        var isActive:Bool?
        var inputCustomerId: Int?
        
     
    }
    
    
    struct RatingToolVideoQuestionModel : Decodable {
        var definition : String?
        var videoQuestionType : Int?
        var questionText : String?
        var videoName : String?
        var videoSizeKB : Int?
        var videoDurationSecond : Int?
        var ratingToolVideoSubjectId : Int?
        var ratingToolVideoSubject : String?
        var videoFile : String?
        var id : Int?
        var createdOn : String?
        var createdById : Int?
        var createdName : String?
        var lastModifiedOn : String?
        var lastModifiedById : Int?
        var lastModifiedName : String?
        var isDeleted : Bool?
        var isActive : Bool?
        var inputCustomerId : Int?
    
     
    }

Error : 

    typeMismatch(Mulakat.RatingToolVideoQuestionModel, 
    Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: 
    "ratingToolVideoInterviewQuestion", intValue: nil), 
    CodingKeys(stringValue: "ratingToolVideoQuestion", intValue: nil)], 
     debugDescription: "Wrong type for JSONNull", underlyingError: nil))


    {
        currentPage = Question;
        currentQuestionNumber = 1;
        isLastQuestion = 1;
        ratingGroupCandidateAnswerStatus =     {
            candidate = "<null>";
            candidateEndTimeOfInterview = "<null>";
            candidateId = 361;
            candidateInterviewStatus = 1;
            candidateStartTimeOfInterview = "2021-01-26T10:17:33.9417242";
            createdById = 1;
            createdName = "Input Teknoloji";
            createdOn = "2021-01-26T10:09:28.1589151";
            id = 437;
            inputCustomerId = 1;
            invitationValidtyEndTime = "2021-02-02T10:08:00";
            invitationValidtyStartTime = "2021-01-26T10:08:00";
            isActive = 1;
            isDeleted = 0;
            isFinish = 0;
            lastModifiedById = 361;
            lastModifiedName = "U00c7aU011fatay Kaya (Aday)";
            lastModifiedOn = "2021-01-26T10:17:33.9417285";
            position = "<null>";
            positionId = 82;
            ratingGroupStage = "<null>";
            ratingGroupStageId = 85;
        };
        ratingToolVideoInterviewQuestion =     {
            createdById = 1;
            createdName = "Input Teknoloji";
            createdOn = "2020-12-22T01:30:24.398776";
            id = 284;
            inputCustomerId = 1;
            isActive = 1;
            isDeleted = 0;
            lastModifiedById = "<null>";
            lastModifiedName = "<null>";
            lastModifiedOn = "<null>";
            order = 0;
            questionTransitionText = "";
            ratingToolVideoInterview = "<null>";
            ratingToolVideoInterviewId = 125;
            ratingToolVideoQuestion =         {
                createdById = 1;
                createdName = "Input Teknoloji";
                createdOn = "2020-12-22T01:22:56.6969327";
                definition = "Sizi en U00e7ok ne motive eder?";
                id = 306;
                inputCustomerId = 1;
                isActive = 1;
                isDeleted = 0;
                lastModifiedById = 1;
                lastModifiedName = "Input Teknoloji";
                lastModifiedOn = "2020-12-22T01:22:56.6969348";
                questionText = "Sizi en U00e7ok ne motive eder?";
                ratingToolVideoSubject = "<null>";
                ratingToolVideoSubjectId = 1;
                videoDurationSecond = 0;
                videoFile = "<null>";
                videoName = "<null>";
                videoQuestionType = 1;
                videoSizeKB = 0;
            };
            ratingToolVideoQuestionId = 306;
            recordingTime = 15;
            thinkingTime = 20;
        };
        ratingToolVideoInterviewQuestionCount = 2;
        videoInterviewEndPageTemplate = "<pre style="text-align: center;">
    
M&uuml;lakata katU0131larak, bilgi ve deneyimlerinizi bizimle paylaU015ftU0131U011fU0131nU0131z i&ccedil;in teU015fekk&uuml;r ederiz.
    

    
S&uuml;recin bundan sonraki aU015famalarU0131 ile ilgili bilgi sahibi olmanU0131zU0131 saU011flamak &uuml;zere, U0130nsan KaynaklarU0131 Ekibimiz sizinle iletiU015fime ge&ccedil;ecek.
    

    
Kariyerinizde baU015farU0131lar dileriz.</pre>
    
";
    }




MY JSON :  
  

      

{
    "ratingToolVideoInterviewQuestion": {
        "ratingToolVideoQuestionId": 306,
        "ratingToolVideoQuestion": {
            "definition": "Sizi en ?ok ne motive eder?",
            "videoQuestionType": 1,
            "questionText": "Sizi en ?ok ne motive eder?",
            "videoName": null,
            "videoSizeKB": 0,
            "videoDurationSecond": 0,
            "ratingToolVideoSubjectId": 1,
            "ratingToolVideoSubject": null,
            "videoFile": null,
            "id": 306,
            "createdOn": "2020-12-22T01:22:56.6969327",
            "createdById": 1,
            "createdName": "Input Teknoloji",
            "lastModifiedOn": "2020-12-22T01:22:56.6969348",
            "lastModifiedById": 1,
            "lastModifiedName": "Input Teknoloji",
            "isDeleted": false,
            "isActive": true,
            "inputCustomerId": 1
        },
        "questionTransitionText": "",
        "recordingTime": 15,
        "thinkingTime": 20,
        "order": 0,
        "ratingToolVideoInterviewId": 125,
        "ratingToolVideoInterview": null,
        "id": 284,
        "createdOn": "2020-12-22T01:30:24.398776",
        "createdById": 1,
        "createdName": "Input Teknoloji",
        "lastModifiedOn": null,
        "lastModifiedById": null,
        "lastModifiedName": null,
        "isDeleted": false,
        "isActive": true,
        "inputCustomerId": 1
    },
    "ratingGroupCandidateAnswerStatus": {
        "positionId": 82,
        "position": null,
        "candidateId": 361,
        "candidate": null,
        "isFinish": false,
        "candidateInterviewStatus": 1,
        "candidateStartTimeOfInterview": "2021-01-26T10:17:33.9417242",
        "candidateEndTimeOfInterview": null,
        "invitationValidtyStartTime": "2021-01-26T10:08:00",
        "invitationValidtyEndTime": "2021-02-02T10:08:00",
        "ratingGroupStageId": 85,
        "ratingGroupStage": null,
        "id": 437,
        "createdOn": "2021-01-26T10:09:28.1589151",
        "createdById": 1,
        "createdName": "Input Teknoloji",
        "lastModifiedOn": "2021-01-26T10:17:33.9417285",
        "lastModifiedById": 361,
        "lastModifiedName": "?a?atay Kaya (Aday)",
        "isDeleted": false,
        "isActive": true,
        "inputCustomerId": 1
    },
    "ratingToolVideoInterviewQuestionCount": 2,
    "currentQuestionNumber": 1,
    "isLastQuestion": true,
    "videoInterviewEndPageTemplate": "<pre style="text-align: center;">
M&uuml;lakata kat?larak, bilgi ve deneyimlerinizi bizimle payla?t???n?z i&ccedil;in te?ekk&uuml;r ederiz.

S&uuml;recin bundan sonraki a?amalar? ile ilgili bilgi sahibi olman?z? sa?lamak &uuml;zere, ?nsan Kaynaklar? Ekibimiz sizinle ileti?ime ge&ccedil;ecek.

Kariyerinizde ba?ar?lar dileriz.</pre>
",
    "currentPage": "Question"
}
question from:https://stackoverflow.com/questions/65918798/swiftui-json-decode

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

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

...