Swift 2.0 How to parse JSON?

I encode the executioner game and load possible words into my application using json text files. I tried to follow the examples of others on this site, but I am getting errors from Xcode.

I tried the following code based on another answer:

import Foundation

var error: NSError?
let jsonData: NSData = /* get your json data */

let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil,   error: &error) as NSDictionary

But I got errors on line 4 with jsonDict, which said that "the call can be dropped, but not marked try, but the error is not processed" and "JSONReadingOptions type does not comply with the NilLiteralConvertible protocol".

Here is the JSON file that I would like to parse:

{
"wordList" : {
    "difficulty" : "Easy"
    "list" : [
        "fireplace",
        "apple",
        "january",
        "tooth",
        "cookies",
        "mysterious",
        "essential",
        "magenta",
        "darling",
        "pterodactyl"
    ]}}

I would like to be able to enter my array of arrays and get the values. Thank you so much for any help!

+4
source share
1 answer

Swift 2 API NSError:

do {
  let jsonDict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions(rawValue: 0)) as? NSDictionary
  if let jsonDict = jsonDict {
     // work with dictionary here
  } else {
     // more error handling
  }
} catch let error as NSError {
  // error handling
}

nil options, NSJSONReadingOptions.

, JSON Swift , Argo, JSON Swift.

+5

Source: https://habr.com/ru/post/1599194/


All Articles