How to parse an array of JSON from database in swift


How to parse an array of JSON from database in swift



In my app I'm trying to parse a JSON file.



This is my JSON:



[ { "locals": [{"name": "Mission Chinese Food", "localDescription": "A", "photograph": "img_02.jpg", "address": "171 E Broadway, New York, NY 10002", "timetable": "Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00", "numberOfReviews": 10, "numberInCity": 2, "cityName": "New York", "ratingValue": 5 }, { "locals": [{"name": "Mission Chinese Food", "localDescription": "A", "photograph": "img_02.jpg", "address": "171 E Broadway, New York, NY 10002", "timetable": "Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00", "numberOfReviews": 10, "numberInCity": 2, "cityName": "New York", "ratingValue": 5 }, ]



This is my swift file:


let url = URL(string: "https://www.litos.srl/showtime/dbGetLocal.php")

do {
let data = try Data(contentsOf: url!)
print(data[0])

guard let rootObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String:AnyObject]] else {
return locals
}

print(rootObject)
let localsObjects = rootObject[0]["locals"] as? [[String:AnyObject]]

} catch {
print(error)
return locals
}
return locals
}



This is the error that occurs in Xcode when I build and run the code:



60
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
2018-06-29 09:44:19.293880+0200 eventsProject[13504:4365358] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-06-29 09:44:19.294067+0200 eventsProject[13504:4365358] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-06-29 09:44:19.294258+0200 eventsProject[13504:4365358] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert



How can I parse my JSON and get data on swift?





The JSON is corrupted, there are 3 opening brackets [ but only 1 closing ] and one extraneous comma as second last character.
– vadian
2 days ago



[


]




3 Answers
3



enter image description hereYou data is invalid.It should not end with , before ] bracket.



Try this one. This will work if your JSON is valid.



For Example.



[ { "locals": [
{"name": "Mission Chinese Food", "localDescription": "A", "photograph": "img_02.jpg", "address": "171 E Broadway, New York, NY 10002", "timetable": "Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00", "numberOfReviews": "10", "numberInCity": "2", "cityName": "New York", "ratingValue": "5" }]}]


if let data = JSON as? NSArray {
for data in data {
if let data = data as? [String: AnyObject] {
if let data = data["locals"] as? NSArray {
for data in data {
if let data = data as? [String: AnyObject] {

}
}
}
}
}
}





Thank you for your answer, but this i the error that occur: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
– Carlo
2 days ago





JSON is not in correct format. Try it in jsoneditoronline.org
– Manish Mahajan
2 days ago



Your Url Response data is missing functionality please check, there is no proper end of dictionary.



Below is your Json response



{"id":0, "name": "Mission Chinese Food", "localDescription": "A ", "photograph": "img_02.jpg", "address": "171 E Broadway, New York, NY 10002", "timetable": "Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00", "numberOfReviews": 10, "numberInCity": 2, "cityName": "New York", "ratingValue": 5 }
stdClass Object ( [locals] => 0 ) { "id":1, "name": "Mission Chinese Food", "localDescription": "B ", "photograph": "img_02.jpg", "address": "171 E Broadway, New York, NY 10002", "timetable": "Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00", "numberOfReviews": 10, "numberInCity": 2, "cityName": "New York", "ratingValue": 5 }
stdClass Object ( [locals] => 0 ) { "id":2, "name": "Jubba the Hutt", "localDescription": "V ", "photograph": "img_02.jpg", "address": "171 E Broadway, New York, NY 10002", "timetable": "Lunedi al Sabato 08:00-12:00 e 14:00- 18:00 Domenica 08:00-12:00 e 14:00- 18:00", "numberOfReviews": 10, "numberInCity": 2, "cityName": "New York", "ratingValue": 5 }
stdClass Object ( [locals] => 0 )
stdClass Object ( [locals] => 0 )






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