I have a text file with 8000+ lines of information. I want to parse it to JSON for using it in iOS application. Because parsing that kind of txt file takes about 35 seconds, which is not good for user experience.
Text file goes like this:
TURKIYE;ADANA;ADANA;36,9914194;35,3308285;03:00:00
TURKIYE;ADANA;ALADAĞ;37,545348;35,394608;03:00:00
TURKIYE;ADANA;CEYHAN;37,028765;35,8124309;03:00:00
TURKIYE;ADANA;FEKE;37,814467;35,910391;03:00:00
TURKIYE;ADANA;İMAMOĞLU;37,2984443;35,6095474;03:00:00
TURKIYE;ADANA;KARAISALI;37,2758825;35,1268781;03:00:00
TURKIYE;ADANA;KARATAŞ;36,6649776;35,2587964;03:00:00
I just want the "TURKIYE", "ADANA", "ALADAĞ", "Latitude" and "Longitude" parts and don't need the last "03:00:00" part.
Edit: Forget the mention about something. Text files some lines doesn't includes third column. For example;
AVUSTURYA;HORBRANZ;47,5557073;9,7525947;01:00:00
AVUSTURYA;HORN;48,66607;15,65716;01:00:00
AVUSTURYA;IMST;47,24013;10,73954;01:00:00
AVUSTURYA;IMSTERAU;47,21018;10,70901;01:00:00
I want to parse them like "if third column doesn't exist print nil value there."
My txt parsing code is:
let textFilePath = Bundle.main.path(forResource: "LatLongData", ofType: "txt")
let fileManager = FileManager.default
if fileManager.fileExists(atPath: textFilePath!) {
do {
let fullText = try String(contentsOfFile: textFilePath!, encoding: String.Encoding.utf8)
let lines = fullText.components(separatedBy: "\n") as [String]
for line in lines {
let data = line.components(separatedBy: ";")
let locationData = LocationData()
if data.first == "TURKIYE" || data.first == "ABD" {
locationData.country = data[0]
locationData.city = data[1]
locationData.district = data[2]
locationData.latitude = data[3]
locationData.longitude = data[4]
locationData.compoundKey = "\(data[0])-\(data[1])-\(data[3])"
} else {
locationData.country = data[0]
locationData.city = data[1]
locationData.latitude = data[2]
locationData.longitude = data[3]
locationData.compoundKey = "\(data[0])-\(data[1])-\(data[3])"
}
locationData.writeToRealmDB()
}
} catch let error as NSError {
print(error.localizedDescription)
}
How can I convert this text to JSON for using in iOS app?
Thanks in advance.