0

I am trying to parse data form an API.

I can print the JSON list but I cannot use any data from it because it has this weird style:

(
    {
        name = "George George";
    }
)

I am currently using this to parse the Data in Swift 3.1:

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        if error != nil {
            print(error!)
        } else {
            if let urlContent = data {
                do {
                    let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    print(jsonResult)
                    print(jsonResult["name"] as AnyObject)
                } catch {
                    print("JSON Processing Failed")
                }
            }
        }
    }
    task.resume()

The print(jsonResult["name"] as AnyObject) should return the name but it just fails

What do I need to do to parse the name?

Pavlos
  • 906
  • 1
  • 11
  • 29
  • 1
    Possible duplicate of [Correctly Parsing JSON in Swift 3](http://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) –  Feb 26 '17 at 03:13
  • @Sneak Nop.. the question you mentioned has nothing to do with this situation – Pavlos Feb 26 '17 at 03:16
  • 1
    How about this? http://stackoverflow.com/questions/38155436/json-parsing-in-swift-3 http://stackoverflow.com/questions/39939143/parse-json-response-with-swift-3 http://stackoverflow.com/questions/39458836/parsing-json-using-swift-3 http://stackoverflow.com/questions/39609726/swift-3-json-parsing http://stackoverflow.com/questions/40378048/how-to-parse-json-object-in-swift-3 http://stackoverflow.com/questions/39759998/parsing-json-into-an-array-in-swift-3 http://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3/39423764 –  Feb 26 '17 at 03:22
  • @Sneak the problem is the structure of the API and not the call – Pavlos Feb 26 '17 at 03:25
  • I don't code in Swift, but I see this is due to your jsonResult is cast wrong. If you read the threads I linked : if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? [[String:AnyObject]] **also** when you type in AnyObject for the print, you should cast it accordingly: as! [String:Any] –  Feb 26 '17 at 03:33
  • I figured this out.. If the API response looks like this: `( { name = "George George"; } )` That means the API response is wrong and the perfect way is to fix the response from the Back-End and not trying to parse data from a wrong API response. There is no reason to waste time on trying to parse data from a wrong API response. @Sneak you can answer this question yourself because I am not able to do it.. Thank you for your help! – Pavlos Feb 26 '17 at 16:28
  • Great that it solved, no worries, I will upvote your question so you can answer it. –  Feb 26 '17 at 16:41
  • 1
    @Sneak thank you so much – Pavlos Feb 26 '17 at 16:42

1 Answers1

1

I figured this out.. If the API response looks like this:

(
    {
        name = "George George";
    }
)

That means the API response is wrong and the perfect way is to fix the response from the Back-End and not trying to parse data from a wrong API response.

There is no reason to waste time on trying to parse data from a wrong API response.

Pavlos
  • 906
  • 1
  • 11
  • 29