6

Is it possible to get a JSON var from XML on swift? I already get xml values with alamofire and SWXMLHash with the below code, but requirements said I must convert it to JSON, is there a way to achieve this?

Alamofire.request(strURL, parameters: nil)
        .responseData { response in
            let xml = SWXMLHash.parse(response.result.value!)
            if let text=xml["NewDataSet"]["Table"][0]["Book"].element?.text{
                print(text)

            }
    }
kary22
  • 61
  • 2
  • 2
  • 6
    You are already half way there. Simply use `JSONSerialization` to convert your `xml` variable into JSON. But why do you need JSON? You already have your XML data as a dictionary. – rmaddy Dec 18 '17 at 22:12
  • 1
    Ty for the answer, I'm trying with JSONSerialization. Yeah idk why, but is a requirement. – kary22 Dec 18 '17 at 22:42
  • You can checkout this link, https://stackoverflow.com/questions/45609447/how-to-convert-xml-and-json-data-in-swift-3 – vikass ninawe Aug 25 '19 at 17:40

0 Answers0