"Swift" Parse json .. getting response but not true in swift 5

Viewed 56

This is input of Api:

{
    "key" : "1",  // Questionnaire response : key
    "answer" : { // Questionnaire response :  options value
        "0":"19 to 27,10",
        "1":"Below 5 years,10",
        "2":"Second class upper & above,10",
        "3":"Yes,10",
        "4":"Below 2 years,10"
    }
}

This is func when iam calling Api:

func callEligibility(with model: [VisaEligibility], completion: @escaping(Result<[VisaEligibility], Error>)-> ()) {
    guard let url =  URL(string: eligibility_url)
    else {
        return     }
    let body: [String: Any] = [
        "key": model[0].key,
        "answer": model[0].answer
    ]
    let finalBody = try? JSONSerialization.data(withJSONObject: body)
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.httpBody = finalBody
    request.allHTTPHeaderFields = ["Token":"birdview@v1"]
    URLSession.shared.dataTask(with: request){
        (data , response, error)  in
        guard let data = data, error == nil else {
            print(error?.localizedDescription ?? "No data")
            completion(.failure(error?.localizedDescription as! Error))
            return
        }
        guard let response = response else {
            return
        }
        print(response)
        let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
        if let responseJSON = responseJSON as? [String: Any] {
            print(responseJSON)
        }
    }.resume()
}

This is Model:

struct VisaEligibility: Codable, Hashable {
    let key: String
    let answer: [String: String]
}

This are answer outputs:

["[\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]": "[\"22 to 29\", \"Bachelor’s degree\", \"Yes\", \"Yes\", \"Yes\", \"3 years or more\", \"Yes\"],[\"20\", \"5\", \"10\", \"0\", \"0\", \"5\", \"5\"]"]
["0", "1", "2", "3", "4", "5", "6"],["22 to 29", "Bachelor’s degree", "Yes", "Yes", "Yes", "3 years or more", "Yes"],["20", "5", "10", "0", "0", "5", "5"]

This are my answer input

Here I am calling Api,

QuestionCount,QuestionName and QuestionPoins are my textfields text

  let answerr =  ["\(questionCounts)":"\(questionNames),\(questionPoints)"]
    let model = [VisaEligibility(key: "45", answer: answerr)]
    ApiManager.shared.callEligibility(with: model) { result in
        switch result {
        case.success(let answ):
            print(answ)
            self.eligiblity = answ
            self.percentLabel.text = answ[0].key
        case.failure(let error):
            print(error)
        }
    }

When iam calling api it should calculate my textfieldt text each of text has own point and it shuold return answer.but it just shows result is 0%

Headers {
    "Cache-Control" =     (
        "no-store, no-cache, must-revalidate"
    );
    Connection =     (
        "Keep-Alive"
    );
    "Content-Type" =     (
        "text/html; charset=UTF-8"
    );
    Date =     (
        "Thu, 08 Sep 2022 13:12:35 GMT"
    );
    Expires =     (
        "Thu, 19 Nov 1981 08:52:00 GMT"
    );
    "Keep-Alive" =     (
        "timeout=5, max=100"
    );
    Pragma =     (
        "no-cache"
    );
    Server =     (
        Apache
    );
    "Transfer-Encoding" =     (
        Identity
    );
} }

["code": 200, "result": 0%]

enter image description here

when iam calling it in postmant it shows me true result. what should i do?

this is questions api's input which i am calling questions and after calling questions i have to pass them like dictionary to another api for to get result

{

  "code": "200",
    "result": [
        {
            "key": "2",
            "questions": [
                {
                    "key": "0",
                    "question": "How old are you? ",
                    "options": [
                        {
                            "option": "22 to 29",
                            "point": "20",
                            "plus": 0
                        },
                        {
                            "option": "30 to 34",
                            "point": "10",
                            "plus": 0
                        },


**problem is in my textfields give's me**
Count["[\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]":Name"[\"22 to 29\", \"Bachelor’s degree\", \"Yes\", \"Yes\", \"Yes\", \"3 years or more\", \"Yes\"],[\"20\", \"5\", \"10\", \"0\", \"0\", \"5\", \"5\"]"]
    **Point**["0", "1", "2", "3", "4", "5", "6"],["22 to 29", "Bachelor’s degree", "Yes", "Yes", "Yes", "3 years or more", "Yes"],["20", "5", "10", "0", "0", "5", "5"]
    but i need they should be.
     **["0":"22 to 29 , 20" ]**
    **[TfCount first:TfName first,Tfpoint first]**
0 Answers
Related