2017-08-10 41 views
0

我是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}] 
+2

在你的問題的回答是一個純粹的'JSON'響應,哪裏是'XML'一部分?你的問題是什麼,好像你已經在做解析了? –

+0

好吧,先生得到它。我想存儲這個數據在不同的array.like在一個數組中的id和其他數組中的imageName –

+0

如何從此響應檢索數組? –

回答

0

這是一個工作的例子,我在操場進行了測試。您需要先將JSON String轉換爲Data對象,然後解析它。

let jsonString = "[{\"Id\":2,\"imgName\":\"_U11tmp1464839741959976567.jpg\",\"SeqNo\":1},{\"Id\":1,\"imgName\":\"_U11tmp1464839741959976567.jpg\",\"SeqNo\":2},{\"Id\":3,\"imgName\":\"_U11tmpIMG-14117-WA59976567.jpg\",\"SeqNo\":3}]" 
guard let jsonData = jsonString.data(using: .utf8) else {return} 
guard let jsonResponse = (try? JSONSerialization.jsonObject(with: jsonData)) as? [[String:Any]] else {return} 
let idArray = jsonResponse.flatMap{$0["Id"] as? Int} 
let imageNames = jsonResponse.flatMap{$0["imgName"] as? String} 

要投入你的代碼的情況下這樣的:

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String, qualifiedName qName: String, attributes attributeDict: [AnyHashable: Any]){ 
    currentElement = elementName 
    guard let jsonData = currentElement.data(using: .utf8) else {return} 
    guard let jsonResponse = (try? JSONSerialization.jsonObject(with: jsonData)) as? [[String:Any]] else {return} 
    let idArray = jsonResponse.flatMap{$0["Id"] as? Int} 
    let imageNames = jsonResponse.flatMap{$0["imgName"] as? String} 
} 
+0

我已檢查它,但它會給出錯誤,當我使用這個讓idArray = jsonResponse.flatMap {$ 0 [「Id」]} –

+0

錯誤是類型沒有下標成員 –

+0

我正在使用我的響應而不是此代碼的響應警衛讓jsonResponse =(嘗試?JSONSerialization.jsonObject(from:data))爲? [[String:Any]] else {return} –