2016-11-04 59 views
1

這裏是數據swift3 Alamofire XML數據

Data: <?xml version="1.0" encoding="utf-8"?> 
<string xmlns="http://192.168.80.41:8081/WebServiceTest.asmx">[ 
    { 
    "NonCID": "n10909", 
    "Name": "aaaa", 
    "ClassType": "aa", 
    "City": "aaa", 
    "Area": "aaa", 
    "Address": "aaaa", 
    "TelArea": "02", 
    "Tel": "29857456", 
    "Latitude": 25.062047000000000, 
    "Longitude": 121.495241000000000 
    }, 
    { 
    "NonCID": "n10957", 
    "Name": "xxxx", 
    "ClassType": "xx", 
    "City": "xxx", 
    "Area": "xxx", 
    "Address": "xxxxx", 
    "TelArea": "02", 
    "Tel": "29736693", 
    "Latitude": 25.061227100000000, 
    "Longitude": 121.495149900000000 
    }, 
    { 
    "NonCID": "n10958", 
    "Name": "xxxx", 
    "ClassType": "xx", 
    "City": "xxx", 
    "Area": "xxx", 
    "Address": "xxxx", 
    "TelArea": "02", 
    "Tel": "29821024", 
    "Latitude": 25.061812900000000, 
    "Longitude": 121.495274000000000 
    }]</string> 

這裏是我的代碼

Alamofire.request("http://localhost/WebServiceTest.asmx/") .responseData { response in 
      debugPrint("All Response Info: \(response)") 
       if let data = response.result.value, let utf8Text = String(data: data, encoding: .utf8) { 
       print("Data: \(utf8Text)") 

      } 
     } 

當我運行這段代碼獲取數據,如我工作,我無法將數據表單[{「noncid」:「xxxx」,「Name」:「bbb」,........ etc}]轉換爲字典。

有誰知道我該如何獲取數據到字典,我可以使用。 或有另一種方式可以做到這一點。謝謝大家的支持

回答

1

使用NSXMLParser解析與string XML元素名稱關聯的文本。然後,一旦您將該文本顯示爲JSON,請使用NSJSONSerialization解析JSON字符串以外的內容。

func parse(data: Data) { 
    // parse the xml 

    let parser = XMLParser(data: data) 
    parser.delegate = self 
    guard parser.parse() else { 
     print("xml parse error: \(parser.parserError?.localizedDescription)") 
     return 
    } 

    // get the parsedXMLString from 

    guard let value = parsedXMLString, let jsonData = value.data(using: .utf8) else { 
     print("cannot convert to data") 
     print("string: \(parsedElementValue)") 
     return 
    } 

    // try now parsing the JSON body 

    do { 
     guard let responseObject = try JSONSerialization.jsonObject(with: jsonData) as? [[String: Any]] else { 
      print("Unable to parse array of dictionaries from JSON") 
      print(value) 
      return 
     } 

     // use responseObject here 

     print(responseObject) 
    } catch let jsonError { 
     print("Unable to parse json: \(jsonError)") 
    } 
} 

如果你有幾個特性:

fileprivate var parsedXMLString: String? 
fileprivate var parsedElementValue: String? 

你實現對XML解析器的擴展:

extension ViewController: XMLParserDelegate { 

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { 
     if elementName == "string" { 
      parsedElementValue = "" 
     } 
    } 
    func parser(_ parser: XMLParser, foundCharacters string: String) { 
     parsedElementValue?.append(string) 
    } 

    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { 
     if elementName == "string" { 
      parsedXMLString = parsedElementValue 
      parsedElementValue = nil 
     } 
    } 
} 

我必須說,雖然,這是一個非常奇怪的格式,在XML中看到JSON。通常你有一個或另一個...

+0

它的工作!!感謝很多歡呼聲 – Jeff