0

I'm stuck, I have an api sending me an array with string and integer inside and not be able to parse it , how do I do that ? thanks

this is a simple format of my Object

let name : String
let data : [String/Int]

and the json is

name : Jhon,
data : [0,1,2,"3","4"]
  • It isn't valid json. Can you get the back end developer to fix their code? Otherwise you are going to need to write custom decoding code that examines each element and parses it accordingly. Will the string values always be integers? – Paulw11 Jun 26 '21 at 23:25
  • @Paulw11 hi, back end develop doesn't want to fix it , so I'm looking to something to read it , I was thinking in custom decoding code but I don't know how to do it, help me – RUBEN OMAR CAMPOS VAZQUEZ Jun 26 '21 at 23:36
  • @Paulw11 It's entirely legal to have JSON arrays of heterogeneous types. It's insane, but legal. – Alexander Jun 26 '21 at 23:37
  • JavaScript is insane in many cases. @RUBENOMARCAMPOSVAZQUEZ one approach would be to declare your array as `[Any]` and then post-process that array or you can use this approach with `Decodable` https://stackoverflow.com/questions/48297263/how-to-use-any-in-codable-type – Paulw11 Jun 26 '21 at 23:44
  • @Paulw11 You could even tuck that decoding logic into a property writer, so you could have something like `@NonsenseArrayOfIntsAndStringInts data: [Int]`, and have it hide the batshit insanity behind a veneer – Alexander Jun 26 '21 at 23:47
  • @Paulw11 i have read it and solved my problem when value is string or int , but I don't know how to do it when is an array – RUBEN OMAR CAMPOS VAZQUEZ Jun 27 '21 at 15:08
  • @Alexander could you explain it more? I'm interested in your answer – RUBEN OMAR CAMPOS VAZQUEZ Jun 27 '21 at 15:09
  • @RUBENOMARCAMPOSVAZQUEZ Have a look at the codable property wrappers defined in https://github.com/GottaGetSwifty/CodableWrappers , and see how they're implemented. You can use the same technique to decorate your own `@NonsenseArrayOfIntsAndStringInts var data: [Int]`. LMK if you have any questions – Alexander Jun 27 '21 at 19:47

0 Answers0