Homework 9: Load a json feed of the class

As the title suggests, in this lesson you will learn how to pull json data from the web and put it to work in your app. You are also going to submit information about yourself into the same database that generates the json feed so that the app  you create will display information about you and your classmates!

The first part of this lesson shows how to extract ‘flat’ data from a json feed, where the data is only one level deep such as last_name“Aldridge” . The process for accessing embedded data such as the estimated_diameter_min value shown below can be significantly more difficult. If you are really interested in geeking out on json feeds, Chapter 21 in our textbook has a LOT of information for you.

The easy part: visit the feed and add your data

  1. Click Here to see the json feed we are going to use. The feed will most likely look like a big jumble and, if you are the first student to visit this lesson this semester, it may only have my information. To make it more readable
    1. Make sure you are using the Chrome browser
    2. Visit https://chrome.google.com/webstore/category/extensions , search for json, and add the JSON view extension. Now Click Again to see the nicely formatted data. Look carefully at that data; especially at the name values, such as first_name: “Tara” shown below. Soon you are going to use those names in your xcode project to pull data.

  2. Now submit YOUR information from this link.  By the way, the data for section and counter are generated automatically.
  3. Click Again and scroll down to see if your data appears.

The less easy part: getting the data into Xcode

  1. Create a new swifUI project and open ContentView.swift
  2. Import Foundation and Combine

  3. Create the DataLayout struct shown below at the bottom of your document. As you can see, the enum CodingKeys must match the data from the json feed: counter, first_name, last_name, and fact.

  4. Create the GetData class shown below. You can add it above or below your DataLayout struct. Position is irrelevant.

  5. Add the load function to GetData (between the brackets).
    1. As you can see, the dataUrl on the first line of the load function is the same url that you just visited.
    2. On line two, you create a JSONDecoder that does the work of sorting out the JSON data from the feed.
    3. Line three, starting with URLSession.shared.dataTask is the most complex part of the function. Best I can tell, it uses JSONDecoder along with the DataLayout struct to process the data from dataURL.

    4. Create the @Published variable students (based on the DataLayout struct) and initialize the load function as shown below.

    5. Create the studentInfo variable as shown below.

    6. The rest is Easy!!! All you have to do is replace the default  Text(“Hello, World!”) with a list that draws from arrays tied to the studentInfo variable that you just created. The screen capture below should serve as a good starting point. Please note that student.name comes directly from the dataLayout struct that you created earlier. Your job now is to get everyone’s last name and ‘facts’ by using the other variables in that struct. When you are finished, your list should resemble this one except that it will be much longer once the entire class has submitted their information.