0

I am trying to call a get web service using Alamofire4.0 and swift 3.0, but it always show me invalid json error, but when I test the same api in postamn is shows perfect response also I have validated json and all online forms validated that.

I found that calling

Alamofire.request(apiURLStr, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseString { (responseObject) in

            var jsonString = responseObject.result.value

            jsonString = jsonString?.replacingOccurrences(of: "\n", with: "")


        }

I have to replace "\n" manually but I want this inbuilt and serialise json data as in postman.

this is my json that am getting from Alamofire :

\n{\n \"id\": 22,\n \"category\": 4,\n \"heading\": \"Sebastian Vettel wins tense Bahrain Grand Prix, Lewis Hamilton second\",\n \"subHeading\" : \"Sebastian Vettel claimed the outright lead of the Formula One championship by winning a tense Bahrain Grand Prix, despite a late charge from Lewis Hamilton.\",\n \"text\" : \"SAKHIR, Bahrain -- Sebastian Vettel claimed the outright lead of the Formula One championship by winning a tense Bahrain Grand Prix, despite a late charge from Lewis Hamilton.\",\n \"picture\" : \"http://www.sebastianvettel.de/images/Galerie-Modul/2017/2017-04-30-GP-Russland.jpg\",\n \"company\" : \"Ferrari\",\n \"author\" : \"Neil\",\n \"published\" : \"2017-04-19 20:20:22\", \n \"updated\" : \"2017-04-19 20:20:22\",\n \"visible\" : \"Y\" \n}

Thanks in advance

Sandeep Jangir
  • 412
  • 5
  • 14
  • I believe you should have a look at this answer here https://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift – TNguyen Jun 13 '17 at 15:50
  • `I have to replace "\n" manually` You actually never have to do that. This is an XY problem. You see backslashes in the printed response string so you think you need to remove them, but you don't. Please show a snippet of your JSON and/or the exact error message you get. :) – Eric Aya Jun 13 '17 at 16:28
  • @EricAya question updated – Sandeep Jangir Jun 13 '17 at 16:42
  • I just noticed you're using `.responseString`. This is very probably the source of the error. You should try with `.responseJSON` (I haven't used Alamofire in a long time but I think it's called like that). The `\n` you see are there because the string is "pretty-printed". But this is only a *representation* of the JSON data. Instead of decoding as string, decode as JSON, it should work. – Eric Aya Jun 13 '17 at 16:44
  • @EricAya when I use .responseJSON it shows unsupported json – Sandeep Jangir Jun 13 '17 at 16:47
  • 1
    It means that the JSON is not standard JSON. Probably because of the *first* `\n` which makes indeed this JSON invalid. If you have possibility of changing that on the server it's the best option. Otherwise, yeah, remove the `\n` from the string *then* decode it as JSON with JSONSerialization or SwiftyJSON. – Eric Aya Jun 13 '17 at 16:48
  • when I use the same url in browser json seems perfect and also I have verified json in various online validatore – Sandeep Jangir Jun 13 '17 at 16:50
  • I'm experiencing the same problem, json validation sites and postman says these are valid jsons while alamofire does not. – Joshua Vidamo Apr 13 '18 at 07:41

0 Answers0