JSON Array is not getting parsed and getting error code due to special characters and how to ignore them?

試著忘記壹切 提交于 2019-12-13 04:26:40

问题


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

来源:https://stackoverflow.com/questions/56701151/json-array-is-not-getting-parsed-and-getting-error-code-due-to-special-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!