0

I am fetching data from an API using OAuth for authorization. I have successfully fetched the data I'm interested in and want to parse the result. It is not possible to convert the response to JSON format or a dictionary.

I have tried using OAuthSwift (https://github.com/OAuthSwift/OAuthSwift):

     // do your HTTP request without authorize
    oauthswift?.client.get(myURL) { result in
        switch result {
        case .success(let response):
        do{
                //here dataResponse received from a network request
                let jsonResponse = try JSONSerialization.jsonObject(with:
                    response.data, options: [])
                print(jsonResponse) //Response result
        } catch let parsingError {
                print("Error", parsingError)
            }
            print(response.string)
        case .failure(let error):
        print(error)
        }
    }

In this case response is in the format OAuthSwiftResponse which I can access .data from (Data)

My problem is that I cannot do JSONSerialization with the data, I get this error message:

    JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set

I want to have a dictionary with the result to be able to get specific values from the JSON object. Thanks in advance!

Casper Lindberg
  • 600
  • 1
  • 6
  • 21
  • 1
    What is your json response? If it is a dictionary why not cast the result, `if let jsonResponse = try JSONSerialization.jsonObject(with: response.data, options: []) as? [String: Any] {...` or use `Codable` instead? – Joakim Danielson Aug 09 '19 at 15:49
  • Found out the response was in XML-format. Thanks anyway. This post can be closed now, not sure how to do it though. – Casper Lindberg Aug 15 '19 at 00:44

0 Answers0