0

I am parsing a json array and i am getting the data right but if the json array contains any special characters like & etc. its getting error code. I will share you my code please check and help me out. Thanks

//XML Parsing

let LocationMessage = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='http://tempuri.org/'><SOAP-ENV:Body><ns1:GetBROMCoordinateByLatLogRad><ns1:latitude>\(place.coordinate.latitude)</ns1:latitude><ns1:logitude>\(place.coordinate.longitude)</ns1:logitude><ns1:radius>200</ns1:radius></ns1:GetBROMCoordinateByLatLogRad></SOAP-ENV:Body></SOAP-ENV:Envelope>"


let LocationurlString = "http://104.xxx./xyzapiservice/API/xyzService.svc"

let LocationmsgLength = String(LocationMessage.count)
let LocationUrl = NSURL(string: LocationurlString)!

var LocationRequest = URLRequest(url: LocationUrl as URL)
LocationRequest.addValue("text/xml", forHTTPHeaderField: "Content-Type")
LocationRequest.addValue("http://tempuri.org/xyxApiService/GetBROMCoordinateByLatLogRad", forHTTPHeaderField: "Soapaction")
LocationRequest.addValue(LocationmsgLength, forHTTPHeaderField: "Content-Length")

LocationRequest.httpMethod = "POST"
LocationRequest.httpBody = LocationMessage.data(using: .utf8)

let Locationsession = URLSession.shared
let Locationtask = Locationsession.dataTask(with: LocationRequest as URLRequest) { (data, response, error) in
    guard let Locationresponse = data,
        error == nil else {
            print(error?.localizedDescription ?? "Response Error")
            return }

    print("Track Response: \(String(describing: response))")

    let Locationparser = XMLParser(data: Locationresponse)
    Locationparser.delegate = self
    Locationparser.parse()
}
Locationtask.resume()

 func parser(_ parser: XMLParser, foundCharacters string: String) {

        if (!string.isEmpty) {
            if currentParsingElement == "GetBROMCoordinateByLatLogRadResult" {
                getLocation = string
                print("Location is",getLocation)

                let jsonData = getLocation.data(using: .utf8)


                do{
                    let jsonResponse = try JSONSerialization.jsonObject(with: jsonData!, options: .allowFragments)
                    print("JSON Data is", jsonResponse) //Response result

                    guard let jsonArray = jsonResponse as? [[String: Any]]
                        else {
                            return
                    }
                        print("JSON Array is", jsonArray)

Error Console:

"BranchType":"Pejabat POS","BranchName":"Pejabat POS Pengkalan Weld","BranchCode":"1052","BranchAddress1":"Unit 1.22 
Error Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}
Location is &
Error Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
Location is  1.23,","BranchAddress2":"Blok U-Shaped 111, Leboh Gat Maccallum,","BranchAddress3":"","Postcode":"10300","City":"Pulau Pinang","Latitude":"5.406208","Longitude":"10
Vignesh Krish
  • 21
  • 1
  • 7
  • May be you can check it out here. https://stackoverflow.com/questions/39699642/alamofire-4-and-special-characters-in-json – Cemal BAYRI Jun 21 '19 at 10:16
  • "Location is &": That's not a valid JSON. Maybe with `.allowFragments`, but definitely not an Array. For the other two, it's not valid JSON (not related to Swift) neither. It's missing at least opening and closing `{` `}` around it. – Larme Jun 21 '19 at 10:25
  • @CemalBAYRI Thanks, but not helping much. I have shared the code and error message, can you help me with editing and post an answer please to ignore special character? – Vignesh Krish Jun 21 '19 at 10:25
  • @Larme I have just given a part of error snippet to show. it has { } around it. but only with & or any other special characters its getting this error but data without any special characters its getting parsed. – Vignesh Krish Jun 21 '19 at 10:33
  • Could you give your full XML file? I'm wondering if it's really good JSON, and we might see why and where it fails exactly. I don't understand why embed JSON in XML. – Larme Jun 21 '19 at 10:35
  • @Larme I have edited the question please check. thanks – Vignesh Krish Jun 21 '19 at 10:39

0 Answers0