Search code examples
iosjsonswiftxcodedecodable

what is the right syntax for a json file?


I wish to populate various collection views and table views with data from a json file. However I do not know how to write that JSON file.

here is the data I wish to populate:

  • 2 sectors : "data" and "developers"
  • each sector contains a number of items:

for "data" :

  • "Data Scientist",
  • "Business Intelligence Analyst (BIA)",
  • "Database Developer",
  • "Database Administrator",
  • "Data Engineer",
  • "Data Analytics Manager",
  • "Data Security Administrator"

for "developer":

  • "APL",
  • "BASIC",
  • "Eiffel",
  • "Frink",
  • "Lisp",
  • "Pascal",
  • "Python",
  • "Ruby",
  • "S-Lang"

How would I need to organize the data so I can later retrieve them ?


Solution

  • To translate the JSON to sections I'd recommend an array

    [
        {"title":"data",
         "items":["Data Scientist","Business Intelligence Analyst (BIA)","Database Developer","Database Administrator","Data Engineer","Data Analytics Manager","Data Security Administrator"]
        },
        {"title":"developer",
         "items":["APL","BASIC","Eiffel","Frink","Lisp","Pascal","Python","Ruby","S-Lang"]
        }
    ]
    

    and decode it to

    struct Section : Decodable {
       let title : String
       let items : [String]
    }
    

    It's the most suitable structure for a table/collection view data source