2015-03-13 120 views
0

我有XML數據在陣列中,如下,即,每行對應於一個單一元件在陣列如何將XML數組轉換爲JSON數據在階

<?xml version="1.0"?> 
<catalog> 
    <book id="bk101"> 
     <author>Gambardella, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <genre>Computer</genre> 
     <price>44.95</price> 
     <publish_date>2000-10-01</publish_date> 
     <description>An in-depth look at creating applications 
     with XML.</description> 
    </book> 
    <book id="bk102"> 
     <author>Ralls, Kim</author> 
     <title>Midnight Rain</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-12-16</publish_date> 
     <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world.</description> 
    </book> 
</catalog> 

如何此xml數組轉換爲JSON格式?

http://scala-tools.org/repo-releases/net/liftweb/

回答

1

下載升JSON罐子一定要抓住正確的庫爲您的斯卡拉版本,在這個時候發佈最新的是位於http://scala-tools.org/repo-releases/net/liftweb/lift-json_2.8.1 /2.3-RC5/

import net.liftweb.json._ 
import net.liftweb.json.JsonAST._ 

val data = xml.XML.loadFile("quotie.xml") 
val str = Printer.pretty(render(Xml.toJson(data))) 

var out_file = new java.io.FileOutputStream("quotie.json") 
var out_stream = new java.io.PrintStream(out_file) 

out_stream.print(str) 
out_stream.close 
+1

但你應該知道電梯有一個錯誤。 http://stackoverflow.com/questions/12019633/converting-xml-to-json-with-lift-behaves-strange – RomKazanova 2015-11-02 14:15:41