How to convert soap string response to json in swift

mentis Source

I'm using SOAP with Swift and I have the response from SOAP. However, the response is of string type and contains JSON array. I'm having problems converting the string to JSON.

let session = URLSession.shared
let task = session.dataTask(with: request){(data,resp,error) in
    guard error == nil && data != nil else{
        print("connection error or data is nil")

        return
    }

    if resp != nil{
        self.mutableData.length = 0;
    }

    self.mutableData.append(data!)

    let xmlParser = XMLParser(data: self.mutableData as Data)
    xmlParser.delegate = self
    xmlParser.parse()
    xmlParser.shouldResolveExternalEntities = true
}

task.resume()
}

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
    currentElementName = elementName
}

func parser(_ parser: XMLParser, foundCharacters string: String) {
    if currentElementName.isEqual(to: "GetStudentGradesResult") {
        datas.append(string)
    }
}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
}

This is my SOAP response string:

[{"StudentId":1526,"SchoolCode":"21013","AcademicPeriod":"2016-2017","ClassBranch":"10-A-LEVEL L","CourseId":1,"CourseName":"Matematik","IsNumber":true,"HomeWork1":10,"Midterm1":9,"Final1":9,"Grade1":9,"HomeWork2":10,"Midterm2":8,"Final2":8,"Grade2":8,"Notes1":null,"Notes2":null,"InstructorIdNumber1":"175023","InstructorIdNumber2":"223153","EffortGrade1":null,"EffortGrade2":null,"LetterGrade1":null,"LetterGrade2":null,"UpdateDate":"2017-07-12T13:42:00","StudentNumber":101,"StudentFullName":"DOĞAÇ TOPRAK İDRİS","StudentObject":{"StudentId":1526,"SchoolCode":"21013","SchoolNumber":101,"FullName":"DOĞAÇ TOPRAK İDRİS","ParentIdNumber":"139206","StudentAcademicList":null}},{"StudentId":1526,"SchoolCode":"21013","AcademicPeriod":"2016-2017","ClassBranch":"10-A-LEVEL L","CourseId":2,"CourseName":"Fizik","IsNumber":true,"HomeWork1":7,"Midterm1":7,"Final1":7,"Grade1":7,"HomeWork2":8,"Midterm2":8,"Final2":8,"Grade2":8,"Notes1":null,"Notes2":null,"InstructorIdNumber1":"165914","InstructorIdNumber2":"168304","EffortGrade1":null,"EffortGrade2":null,"LetterGrade1":null,"LetterGrade2":null,"UpdateDate":"2017-07-12T13:42:00","StudentNumber":101,"StudentFullName":"DOĞAÇ TOPRAK İDRİS","StudentObject":{"StudentId":1526,"SchoolCode":"21013","SchoolNumber":101,"FullName":"DOĞAÇ TOPRAK İDRİS","ParentIdNumber":"139206","StudentAcademicList":null}}]

jsonswiftsoap

Answers

comments powered by Disqus