Parse JSON in Swift 24hours of problems


Parse JSON in Swift 24hours of problems



I am really struggling to parse a json file into my iOS app in Swift.
I have a JSOn output from an SQL db. I think the formatting is wrong and the data headers are a bit ambiguous but my developer who I hired to create the process is happy with the output. Link below.



So in the last 3 days I have read all of the Apple Developer materials on parsing JSON, also watched numerous tutorials which all cover pretty much the same data, and I don't know how much I have read and converted into actual projects which have all failed. I am really needing some guidance here. I have even in a desperate attempt to understand my problem from a different perspective I have looked at how it is done in other languages.
This is the latest iteration:


func flightData()-> [[String: String]]{
let url = URL(string: "http://35.237.114.234/api/index.php?uid=Bx7faf08A9fYJ7bZCNMUX9EzxYN2")

guard let jsonFileURL = Bundle.main.url(forResource: "jsonfile", withExtension: "json") else {fatalError("Failed to get JSON file URl")}
var jsonData: Data!
var jsonDictionary: [String: AnyObject]!

//Getting data from URL
do {
jsonData = try Data(contentsOf: jsonFileURL)
} catch let error as NSError{
print(error.debugDescription)
fatalError("Failed to initiate JSON data from URL!")
}

//getting top level JSON which is "data"
do {
jsonDictionary = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: AnyObject]
} catch let error as NSError{
print(error.debugDescription)
fatalError("Failed to initiate top level JSON dictionary")
}

let memebers: [[String: String]] = jsonDictionary["data"] as! [[String: String]]

for person in data{
print(person["date"]!)
}

return members
}



Here is the main data I am trying to parse.


{"data":[{"date":"Jul01","message":"1877n12:50n13:50nMANnBIOn17:00n(320)nn1878n17:30nBIOnMANn18:30n19:00n(320)nnnnnnnnnnnnnnn4:10n6:10n","notice":""},{"date":"Jul02","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul03","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul04","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul05","message":"1887n12:25n13:25nMANnMUCn16:30nn1888n17:05nMUCnMANn18:15nn1835n19:00nMANnAMSn21:20nn1836n21:50nAMSnMANn22:10n22:40nnnnn6:55n10:15n","notice":""},{"date":"Jul06","message":"1937n15:10n16:10nMANnHERn22:20n(320)nn1938n23:05nHERn(320)nnnnnnnnnnnnnnnnnn8:05n9:50n","notice":""},{"date":"Jul07","message":"MANn01:25n01:55n(320)nn1909n18:40n19:40nMANnPMIn23:20nnnnnnnnnnnnnnnnnnn5:00n7:15n","notice":""},{"date":"Jul08","message":"1910n00:05nPMInMANn01:45n02:15nnRESTnnnnnnnnnnnnnnnnnnnnnn0:45n1:15n","notice":""},{"date":"Jul09","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul10","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul11","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul12","message":"LSBYn10:15n18:15nnnnnnnnnnnnnnnnnnnnnnnnnnnn8:00n","notice":""},{"date":"Jul13","message":"1937n15:10n16:10nMANnHERn22:20n(320)nn1938n23:05nHERn(320)nnnnnnnnnnnnnnnnnn8:05n9:50n","notice":""},{"date":"Jul14","message":"MANn01:25n01:55n(320)nn1851n17:55n18:55nMANn(320)nnnnnnnnnnnnnnnnnnnn5:55n8:00n","notice":""},{"date":"Jul15","message":"JTRn01:00n(320)nn1852n01:35nJTRnMANn03:50n04:20n(320)nnRESTnnnnnnnnnnnnnnnnn2:50n3:20n","notice":""},{"date":"Jul16","message":"1801n12:40n13:40nMANnPRGn16:45n(320)nn1802n17:15nPRGnMANn18:30n19:00n(320)nnnnnnnnnnnnnnn4:20n6:20n","notice":""},{"date":"Jul17","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul18","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul19","message":"1833n13:45n14:45nMANnAMSn17:10n(320)nn1834n17:40nAMSnMANn18:00n(320)nn1877n18:30nMANnBIOn21:40n(320)nn1878n22:10nBIOnMANn23:10n23:40n(320)n6:55n9:55n","notice":""},{"date":"Jul20","message":"1957n14:50n15:50nMANnFNCn19:55n(320)nn1958n20:35nFNCn(320)nnnnnnnnnnnnnnnnnn8:00n10:10n","notice":""},{"date":"Jul21","message":"MANn00:30n01:00n(320)nn1851n17:55n18:55nMANn(320)nnnnnnnnnnnnnnnnnnnn5:30n7:05n","notice":""},{"date":"Jul22","message":"JTRn01:00n(320)nn1852n01:35nJTRnMANn03:50n04:20n(320)nnRESTnnnnnnnnnnnnnnnnn2:50n3:20n","notice":""},{"date":"Jul23","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul24","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul25","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul26","message":"LSBYn10:15n18:15nnnnnnnnnnnnnnnnnnnnnnnnnnnn8:00n","notice":""},{"date":"Jul27","message":"1937n15:10n16:10nMANnHERn22:20n(320)nn1938n23:05nHERn(320)nnnnnnnnnnnnnnnnnn8:05n9:50n","notice":""},{"date":"Jul28","message":"MANn01:25n01:55n(320)nn1851n17:55n18:55nMANn(320)nnnnnnnnnnnnnnnnnnnn5:55n8:00n","notice":""},{"date":"Jul29","message":"JTRn01:00n(320)nn1852n01:35nJTRnMANn03:50n04:20n(320)nnRESTnnnnnnnnnnnnnnnnn2:50n3:20n","notice":""},{"date":"Jul30","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"Jul31","message":"D/Onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn","notice":""},{"date":"","message":"nnnnnnnnnnnnnnnnnnnnnnnnnnnnnBlocknDutyn","notice":""}],



I think the structure is the problem.
Any advice where I am going wrong?



Thanks





So what actually happens when you run this code? Update your question (don't post comments) with details about any messages, errors, etc.
– rmaddy
2 mins ago





feel free to see my post maybe it will help. stackoverflow.com/questions/50420124/… also check out json4swift.com
– owen gerig
1 min ago





Just google for "JSON on-line viewer validator".. You'll notice that your JSON ISN'T formatted correctly - and the viewer should help show you where, and what you can do to fix it.
– paulsm4
43 secs ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV