Show JSON output to screen?

How would I take the output from JSON and print it to the IBOutlet's newsview? Thanks!


class ViewController: UIViewController {

  @IBOutlet var newsview: UITextView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    // API Endpoint:

  let urlString = "[string withheld]"
  let url = URL(string: urlString)
  guard url != nil else {
    debugPrint("URL IS nil")
    return
  }

  let session = URLSession.shared
  let dataTask = session.dataTask(with: url!) { (data, response, error) in

    if error == nil && data != nil {
      let decoder = JSONDecoder()

      do {

        let newsFeed = try decoder.decode(NewsFeed.self, from: data!)

        print("Result: \(newsFeed)")

      //  newsview.text = NewsFeed as? String!
      } catch {

        debugPrint("Error in JSON Parsing!")
    }
  }
}
  dataTask.resume()
}

  override func viewDidAppear(_ animated: Bool) {
}




How is NewsFeed defined ?

It is probably a struct or a class. A clean way to do it is to add inside the struct or class a description var:

struct NewsFeed {

  // your var declaration

  // add this one
  var description : String {
    // build the string, with the linefeeds "\n" if needed and return it
  }
}

Then, in your code:

      newsview.text = newsFeed.description

Oh yeah, that would help.

Here you go:




struct NewsFeed: Codable
{
  var status: String = ""
  var totalResults: Int = 0
  var articles: [Article]?
}

So, I understand you want to show articles.

Could you show how Article is defined ?

The code would be like:

  var description : String {
     var s = ""
     if articles != nil
       for article in articles! {
          if !s.isEmpty {
             s += "\n"
          }
          s += article.content // I don't know how Article is defined
       }
  }

How do you want to use status ?

I don't want to show the status, only the content within the articles.

After looking at your code and looking at some others, I finally got it working to a point. I wish I could show you a screenshot of what I have so far.

Now the articles show up on the simulator, but they are all in raw form. I think I know how to take it from here. Do I put the raw data in a form such as a tableView to be readable?

Thanks.

Now the articles show up on the simulator, but they are all in raw form. I think I know how to take it from here. Do I put the raw data in a form such as a tableView to be readable?

Yes, a tableView is a good way of presenting. It gives a lot of interesting options, as reordering (newest or oldest first) very simply.

Then you should populate a dataSource (an array of string) for the tableView.

  var dataSource : [String] {
     var source = [String]()
     if articles != nil {
        for article in articles! {
           source.append(article.content) // I don't know how Article is defined
        }
      }
      return source
  }

Note: you can now post image on the forum with the add file button at bottom of editing window:

Just take care to reduce the size of image in Preview app, otherwise image is really too large for the forum.

Okay, thanks for the info. It really did help!

Dan

Show JSON output to screen?
 
 
Q