我是IOS中的新成員,我希望將使用swift 3從SOAP Web服務接收的一些混合數據(xml和JSON混合數據)轉換爲數組。解析器方法中的字符串變量。如何在swift中轉換xml和json數據3
func connection(_ connection: NSURLConnection, didFailWithError error: Error){
print("\(error)")
print("Some error in your Connection. Please try again.")
let alert = UIAlertController(title: "Error", message: "No internet connection", preferredStyle: UIAlertControllerStyle.alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func connectionDidFinishLoading(_ connection: NSURLConnection){
print("Received \(UInt(webResponseData.count)) Bytes")
// let theXML = String(webResponseData.mutableBytes, length: webResponseData.length, encoding: String.Encoding.utf8)
let theXML = XMLParser(data: webResponseData)
theXML.delegate = self
theXML.parse()
print("\(theXML)")
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String, qualifiedName qName: String, attributes attributeDict: [AnyHashable: Any]){
currentElement = elementName
// print(currentElement)
}
func parser(_ parser: XMLParser, foundCharacters string: String){
currentElement = string
UserDefaults.standard.set(currentElement, forKey: "string")
//print(currentElement)
// arr.append(currentElement)
}
func parser(_ parser: XMLParser,didEndElement elementName: String, namespaceURI: String?,qualifiedName qName: String?){
let sessionelement = UserDefaults.standard.object(forKey: "string")
print(sessionelement!)
}
這裏是從Web服務響應:
[{"Id":2,"imgName":"_U11tmp1464839741959976567.jpg","SeqNo":1},
{"Id":1,"imgName":"_U11tmp1464839741959976567.jpg","SeqNo":2},
{"Id":3,"imgName":"_U11tmpIMG-14117-WA59976567.jpg","SeqNo":3}]
在你的問題的回答是一個純粹的'JSON'響應,哪裏是'XML'一部分?你的問題是什麼,好像你已經在做解析了? –
好吧,先生得到它。我想存儲這個數據在不同的array.like在一個數組中的id和其他數組中的imageName –
如何從此響應檢索數組? –