Jeff Jeff - 1 month ago 17
JSON Question

SWIFT 3 How to connect Json from my localhost web service

I Would like to connect data from my localhost but there have error
"json error: The data couldn’t be read because it isn’t in the correct format."


here is code.


let session = URLSession.shared
var dataTask:URLSessionDataTask!
var webDomain = "http://192.xxx.xx.xx:80xx/WebServiceTest.asmx"
var url:URL!
var arrTable = [[String:AnyObject]]()
func getTableDataFromWeb(){
strURL = webDomain + "/SearchNonMemberByRangeLatitude=25.062047&Longitude=121.495241&Range=300"
url = URL(string: strURL)
let request = URLRequest(url: URL(string: strURL)!)
let loginTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in guard
let data = data, let _ = response , error == nil else {
print("error")
return
}
do{
let arrayJSON = try JSONSerialization.jsonObject(with: data,options: .allowFragments)as! Dictionary<String, AnyObject>
self.arrTable = [arrayJSON]
print(self.arrTable)
}catch let error as NSError { print("json error: \(error.localizedDescription)") }
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
})
print("Web:\(strURL)")
loginTask.resume()
}



here is the Json data


<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://192.xxx.xx.xx:8081/WebServiceTest.asmx">[
[
{
"NonCID": "n10909",
"Name": "天佑藥局",
"ClassType": "藥局",
"City": "新北市",
"Area": "三重區",
"Address": "大同北路8號",
"TelArea": "02",
"Tel": "29857456",
"Latitude": 25.062047,
"Longitude": 121.495241
},
{
"NonCID": "n10957",
"Name": "德慈藥局",
"ClassType": "藥局",
"City": "新北市",
"Area": "三重區",
"Address": "重新路3段16號1樓",
"TelArea": "02",
"Tel": "29736693",
"Latitude": 25.0612271,
"Longitude": 121.4951499
},
{
"NonCID": "n10958",
"Name": "江村藥局",
"ClassType": "藥局",
"City": "新北市",
"Area": "三重區",
"Address": "重新路3段9號",
"TelArea": "02",
"Tel": "29821024",
"Latitude": 25.0618129,
"Longitude": 121.495274
},
{
"NonCID": "n11023",
"Name": "閎安藥局",
"ClassType": "藥局",
"City": "新北市",
"Area": "三重區",
"Address": "重新路2段79號1樓",
"TelArea": "02",
"Tel": "29881968",
"Latitude": 25.0619308,
"Longitude": 121.4963284
}
]</string>



I also try XMlParse is work but get the data like this


Getting tag:string
Tag detail:["["]
Tag detail:["\n {"]
Tag detail:["\n \"NonCID\": \"n10909\","]
Tag detail:["\n \"Name\": \""]
Tag detail:["天佑藥局\",\n \"ClassType\": \"藥局\",\n \"City\": \"新北市\",\n \"Area\": \"三重區\",\n \"Address\": \"大同北路8號\",\n \"TelArea\": \"02\",\n \"Tel\": \"29857456\",\n \"Latitude\": 25.062047000000000,\n \"Longitude\": 121.495241000000000\n },\n {\n \"NonCID\": \"n10957\",\n \"Name\": \"德慈藥局"]
Tag detail:["\",\n \"ClassType\": \"藥局\",\n \"City\": \"新北市\",\n \"Area\": \"三重區\",\n \"Address\": \"重新路3段16號1樓\",\n \"TelArea\": \"02\",\n \"Tel\": \"29736693\",\n \"Latitude\": 25.061227100000000,\n \"Longitude\": 121.495149900000000\n },\n {\n \"NonCID\": \"n10958\",\n \"Name\": \"江村藥局\",\n "]
Tag detail:[" \"ClassType\": \"藥局\",\n \"City\": \"新北市\",\n \"Area\": \"三重區\",\n \"Address\": \"重新路3段9號\",\n \"TelArea\": \"02\",\n \"Tel\": \"29821024\",\n \"Latitude\": 25.061812900000000,\n \"Longitude\": 121.495274000000000\n },\n {\n \"NonCID\": \"n11023\",\n \"Name\": \"閎安藥局\",\n \"Class"]
Tag detail:["Type\": \"藥局\",\n \"City\": \"新北市\",\n \"Area\": \"三重區\",\n \"Address\": \"重新路2段79號1樓\",\n \"TelArea\": \"02\",\n \"Tel\": \"29881968\",\n \"Latitude\": 25.061930800000000,\n \"Longitude\": 121.496328400000000\n }\n]"]



Here is code


var tagName = String()
var tagContent = String()
func getTableDataFromWeb()
{
strURL = webDomain + "/SearchNonMemberByRange?Latitude=25.062047&Longitude=121.495241&Range=300"
url = URL(string: strURL)!
dataTask = session.dataTask(with: url) { (xmldata, response, error)
-> Void in
if xmldata == nil {
print("dataTaskWithRequest error: \(error)")
return
}
let xmlParser = XMLParser(data: xmldata!)
xmlParser.delegate = self
xmlParser.parse()
print("Web:\(strURL)")
dataTask.resume()
}
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
if elementName == "NonCID"{
tagName = elementName}
print("Getting Tag:\(elementName)")
}
func parser(_ parser: XMLParser, foundCharacters string: String)
{
tagContent = string
arr = [tagContent]
print("Tag Detail:\(arr)")
}
func parserDidEndDocument(_ parser: XMLParser)
{
DispatchQueue.main.async { () -> Void in
self.tableView.reloadData()
}
}


Can someone tell what should I to use Json data plz thanks.

Answer

Your data is XML. It just so happens that the text value of one of the XML elements in the XML file is JSON. So you need your XML parsing code to extract the JSON text. Then you need to use JSONSerialization on the extracted JSON.

Comments