0

I've seen a number of similar questions, but none seem to match my use case.

I have a json file structured as follows:

{
    "Trains": [{
        "Car": "8",
        "Destination": "Glenmont",
        "DestinationCode": "B11",
        "DestinationName": "Glenmont",
        "Group": "1",
        "Line": "RD",
        "LocationCode": "A06",
        "LocationName": "Van Ness-UDC",
        "Min": "3"
    }, {
        "Car": "6",
        "Destination": "Shady Gr",
        "DestinationCode": "A15",
        "DestinationName": "Shady Grove",
        "Group": "2",
        "Line": "RD",
        "LocationCode": "A06",
        "LocationName": "Van Ness-UDC",
        "Min": "3"
    
    }]
}

I'm trying to get the dictionaries for each train. I've tried this (amongst other efforts), but I cant get my head around it. Here is my code:

jsonArray = [try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers)] as! [String]
            
            for train in jsonArray {
                print(train["name"])
            }

This doesn't compile.

My jsonArray is set up as:

 var jsonArray = [Any]()
2
  • 1
    Please read the JSON. It's quite easy. {} is dictionary, [] is array. Therefore the root object is a dictionary ([String:Any]) and the value for key Trains is an array ([[String:Any]]), not [Any]. There is no [String] type at all in the JSON. Commented Oct 30, 2018 at 16:58
  • gaaah, I always switch them. Thanks Commented Oct 30, 2018 at 17:01

1 Answer 1

1

I hope this answer will match your case, check below, Don't get confused I used your JSON response in a file.

if let path = Bundle.main.path(forResource: "file", ofType: "json") {
        do {
            let data1 = try Data(contentsOf: URL(fileURLWithPath: path), options: [])

            let jsonDic = try JSONSerialization.jsonObject(with: data1, options: .mutableContainers) as? [String:Any]
            guard let dic = jsonDic else { return}
            if let dict = dic["Trains"] as? [[String:Any]]{
                print(dict)
            }
        } catch {
            print(error as NSError)
        }

}

If you want to use a decoder then use this.

struct Result: Decodable {
     let Trains:[transaction]
}
struct transaction: Decodable {
    let Car:String
    let Destination:String
    let DestinationCode:String
}
var result = [Result]()

 if let path = Bundle.main.path(forResource: "file", ofType: "json") {
         do {
            let data1 = try Data(contentsOf: URL(fileURLWithPath: path), options: [])
            let decoder = JSONDecoder()
            result = [try decoder.decode(Result.self, from: data1)]
             print(result)
         } catch {
            print(error)
        }
    }

Feel free to say any mistake is there in my coding.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I did solve the problem along the lines of your answer, so I’ll mark the question as complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.