0

I'm following a tutorial at the moment and trying to apply it to something that I can use and understand better then just a general random API and I ran into this issue when trying to Post data. "This data couldn't be read because it isn't in the correct format"

Using Xcode and Swift.

This is the API I'm trying to grab from is https://github.com/ToontownRewritten/api-doc/blob/master/invasions.md http://toontownrewritten.com/api/invasions

I put it formatted under the code snippet

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        fetchPostData { (posts) in
                   
            for post in posts {
                print(post.type)
            }
        }
    }


    func fetchPostData(completionHandler: @escaping ([Post]) -> Void){        
        let url = URL(string: "https://www.toontownrewritten.com/api/invasions")!
    
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        
            guard let data = data else { return }
        
            do {
                let postsData = try JSONDecoder().decode([Post].self, from: data)
            
                completionHandler(postsData)
            
            } catch {
                let error = error
                print(error.localizedDescription)
            }            
        }.resume()        
    }
}

Here is the class that I have for posting the data

import Foundation

struct Post: Codable {
    var invasions: String!
    var type: String!
    var asOf: Int!
    var progress: Int!

}

This is the API I'm trying to grab from formatted. http://toontownrewritten.com/api/invasions

{
   "lastUpdated":1624089300,
   "invasions":{
      "Thwackville":{
         "asOf":1624089291,
         "type":"Big Wig",
         "progress":"477/8000"
      },
      "Splashport":{
         "asOf":1624089283,
         "type":"Short Change",
         "progress":"631/3214"
      },
      "Kaboom Cliffs":{
         "asOf":1624089282,
         "type":"Legal Eagle",
         "progress":"609/4000"
      },
      "Fizzlefield":{
         "asOf":1624089299,
         "type":"Bean Counter",
         "progress":"6141/8000"
      },
      "Boingbury":{
         "asOf":1624089283,
         "type":"Money Bags",
         "progress":"2246/3211"
      }
   },
   "error":null
}
vadian
  • 274,689
  • 30
  • 353
  • 361
asuspro
  • 1
  • 2
  • Where is the code for posting the data? – Tushar Sharma Jun 19 '21 at 08:03
  • @TusharSharma In another class, It's here I've added it to the main post – asuspro Jun 19 '21 at 08:05
  • You should add your definition of `Post`. Right away, I see that you're trying to decode as if you're decoding an array, but the root level of the JSON is *not* an array, which guarantees that the decoding will fail. – jnpdx Jun 19 '21 at 08:05
  • The error is pretty clear. The JSON starts with a brace so the root object is definitely **not** an array. And `invasions` is not a string, it’s a dictionary. Actually your entire model is wrong. – vadian Jun 19 '21 at 08:06
  • I would also suggest you to give different name to model class. From your question it sounds like you were trying to make a post request which got failed. – Tushar Sharma Jun 19 '21 at 08:08
  • @vadian I was following a tutorial for this and completely overlooked that, Is there anywhere you would suggest to learn another way to do it as most the tutorials I can find deal with arrays. I've had luck doing other APIs but those were all similar and went along with the tutorials. – asuspro Jun 19 '21 at 08:18
  • Please read [this answer](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3/39423764#39423764). It describes how to read JSON and the basics to decode it in a general way. And please never never ever declare members in a struct conforming to `Codable` as implicit unwrapped optional. – vadian Jun 19 '21 at 08:22

0 Answers0