UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Milestone Project 13-15

Forums > 100 Days of Swift

@Sammy  

What websites do my fellow swift developers use to create JSON files to use in their projects? Any reputable and commonly used sites?

3      

I don't really think there are going to be websites that will do it for you any faster than just using the text editing program of your choice. Maybe if you already have all the data you want written out in some other type of format like an excel sheet or something it could be converted to JSON automatically. Or, if you already have the objects created in an array in your code, you could always write a function that prints all the objects out in JSON format for you.

But, one way or another, you're going to have to type out the data that you want at some point, unless it is already in some kind file that somebody else has already typed out for you. Even If there is a website that creates the JSON formatting for you, you are still going to have to fill out the data for each object on that website as well, so I don't think it would really save you any time.

However, once you know how to type out the format for one of the objects that you need, you should just be able to copy and paste it as needed to add more objects, and fill in the values with different data on each one.

4      

@Sammy  

Below is the JSON file which is meant to be an erray

{
   "countries":[
      {
         "nation":"Eritrea",
         "capital":"Asmara",
         "population":"6.081 million",
         "size":"117,600 km sq"
      },
      {
         "nation":"Somalia",
         "capital":"Mogadishu",
         "population":"16.360 million",
         "size":"637,657 km sq"
      },
      {
         "nation":"France",
         "capital":"Paris",
         "population":"67.413 million",
         "size":"643,801 km sq"
      },
      {
         "nation":"Spain",
         "capital":"Madrid",
         "population":"47.451",
         "size":"505,990 km sq"
      }
   ]
}

However I get an error saying my JSON is a dictionary when i try to pass it in the JSON decoder as shown below:

func load() {
        if let fileLocation = Bundle.main.url(forResource: "data", withExtension: "json") {

            // do catch in case of an error
            do {
                let data = try Data(contentsOf: fileLocation)
                let jsonDecoder = JSONDecoder()
                let dataFromJson = try jsonDecoder.decode([Country].self, from: data)

                self.countries = dataFromJson
                tableView.reloadData()

            } catch {

                print(error)
            }

        }

    }

This is the error:

2022-12-17 13:05:01.388603-0500 Milestone Project 13-15[5197:152197] [Assert] UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems. navigationController=<UINavigationController: 0x125017c00>, navigationBar=<UINavigationBar: 0x1247064d0; frame = (0 47; 0 50); opaque = NO; autoresize = W; layer = <CALayer: 0x600003aa6d60>> delegate=0x125017c00

typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

3      

Try using this instead.

[
      {
         "nation":"Eritrea",
         "capital":"Asmara",
         "population":"6.081 million",
         "size":"117,600 km sq"
      },

      {
         "nation":"Somalia",
         "capital":"Mogadishu",
         "population":"16.360 million",
         "size":"637,657 km sq"
      },

      {
         "nation":"France",
         "capital":"Paris",
         "population":"67.413 million",
         "size":"643,801 km sq"
      },

      {
         "nation":"Spain",
         "capital":"Madrid",
         "population":"47.451",
         "size":"505,990 km sq"
      }
]

4      

Also, make sure that the data types of the properties in Country match what is in your JSON file. According to your JSON file, they should all be String.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.