【发布时间】:2017-08-10 09:18:57
【问题描述】:
我是 IOS 新手,我想使用 swift 3 将从 SOAP Web 服务接收的一些混合数据(xml 和 JSON 混合数据)转换为数组。 我在解析器方法的字符串变量中接收到这些数据。
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!)
}
这是来自网络服务的响应:
[{"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部分在哪里?你的问题是什么,好像你已经在解析了? -
好的,先生明白了。我想将这些数据存储在不同的数组中。比如 id 在一个数组中,imageName 在另一个数组中
-
如何从这个响应中检索到数组?
-
看看我的回答。你没有告诉我存储
JSON响应的变量的类型,所以我做了一个假设,告诉我是不是别的东西,我会更新我的答案。 -
jason 响应存储在字符串变量中......变量是 currentElement
标签: json xml swift web-services