Showing JSON contents to iOS screen output?

Hi,
I have a json file that I have it printing to the terminal, but now I want to have the output go to an iPhone, iPad, and Apple Watch screen. Here's what I have in the viewDidLoad section....

override func viewDidLoad() {

    super.viewDidLoad()

    

    let data = GetData().$userData

   print(data)

    

    // Immediately get a quote upon app entry.

    //getQuote()

  }

The $userdata and data are defined in another file. Again, everything parses fine in the terminal.

If you need the other part of the code, please let me know.

Thanks,
Dan Uff

If you need the other part of the code, please let me know.

Please show everything you can show.
Here you go....








I hope to delete the switch statements in favor of using the quotes from the JSON file.
Thanks for showing your code.
Many things has gotten clearer, but not enough yet.

I guess you want to show one of the 101 sayings randomly, in your getQuote().
Now you want to move the hard-coded sayings into the JSON file, no?

What I do not understand is...

The property userData of the class GetData is an Array of UserData, and each UserData has two Arrays quote and name.
So, it is a sort of nested Array.

How do you want to choose one quote (or name???) from the nested Array?
Can you show the content of the JSON file quotes.json?
Thanks for the help.

Yes, I do want to randomly show a quote from the json file (but from what I'm hearing, kind of not possible) so I'd settle for showing a quote one at a time.

As for your question, I was going to try and take the information from the json file, put that in an arc4random so it could randomly select a quote to show. Or am I on the wrong track?

Here's the json file....


THANK YOU AGAIN!

Here's the json file....

The first thing I could have found so soon is that your UserData does not represent the data in your JSON.

It should be something like this:
Code Block
import Foundation
struct QuoteData: Codable {
var quote: String //<- each value for key "quote" is a string, not an array
var name: String //<- each value for key "name" is a string, not an array
}

  • I renamed UserData to QuoteData, as it does not seem to represent user.

  • I changed class to struct, as I cannot find any reasons to use class.


Now I can examine your code in detail and find your GetData (not a good name as a type) disposes the result of JSONSerialization and never store it into userData.

You should better use JSONDecoder instead of JSONSerialization, to get the result of the right type without needing conversion.
With making your UserData conforming Codable, I guess you have once tried to use JSONDecoder and failed.

If you have some difficulties in using it, I recommend you to start another thread for it.
Showing JSON contents to iOS screen output?
 
 
Q