2012-03-12 33 views
1

做了很多的谷歌搜索后.....转换的XmlSlurper GPathResult以JSON字符串

是否有转换的XmlSlurper结果(GPathResult)到JSON的“简单的方法”?

我加入,我想获取服务器端,将其存储在一个数据库(JSON),并转储到浏览器页面的JavaScript处理的示例XML文件...

谢谢!

桑德

请参见下面的XML文件.....:

<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"> 
    <opensearch:Query searchTerms="tt0137523"/> 
    <opensearch:totalResults>1</opensearch:totalResults> 
    <movies> 
    <movie> 
     <popularity>3</popularity> 
     <translated>true</translated> 
     <language>en</language> 
     <original_name>Fight Club</original_name> 
     <name>Fight Club</name> 
     <alternative_name>El club de la lucha</alternative_name> 
     <type>movie</type> 
     <id>550</id> 
     <imdb_id>tt0137523</imdb_id> 
     <url>http://www.themoviedb.org/movie/550</url> 
     <overview>A lonely, isolated thirty-something young professional seeks an escape from his mundane existence with the help of a devious soap salesman. They find their release from the prison of reality through underground fight clubs, where men can be what the world now denies them. Their boxing matches and harmless pranks soon lead to an out-of-control spiral towards oblivion.</overview> 
     <votes>15</votes> 
     <rating>8.2</rating> 
     <certification>R</certification> 
     <released>1999-09-16</released> 
     <runtime>138</runtime> 
     <categories> 
     <category type="genre" name="Crime" url="http://themoviedb.org/genre/crime" id="80"/> 
     <category type="genre" name="Drama" url="http://themoviedb.org/genre/drama" id="18"/> 
     <category type="genre" name="Thriller" url="http://themoviedb.org/genre/thriller" id="53"/> 
     </categories> 
     <images> 
     <image type="poster" url="http://hwcdn.themoviedb.org/posters/f84/4bc908ae017a3c57fe002f84/fight-club-mid.jpg" size="mid" id="4bc908ae017a3c57fe002f84"/> 
     <image type="poster" url="http://hwcdn.themoviedb.org/posters/f84/4bc908ae017a3c57fe002f84/fight-club-cover.jpg" size="cover" id="4bc908ae017a3c57fe002f84"/> 
     <image type="poster" url="http://hwcdn.themoviedb.org/posters/f84/4bc908ae017a3c57fe002f84/fight-club-thumb.jpg" size="thumb" id="4bc908ae017a3c57fe002f84"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f55/4bc908a8017a3c57fe002f55/fight-club-original.jpg" size="original" id="4bc908a8017a3c57fe002f55"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f55/4bc908a8017a3c57fe002f55/fight-club-poster.jpg" size="poster" id="4bc908a8017a3c57fe002f55"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f55/4bc908a8017a3c57fe002f55/fight-club-thumb.jpg" size="thumb" id="4bc908a8017a3c57fe002f55"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f51/4bc908a6017a3c57fe002f51/fight-club-original.jpg" size="original" id="4bc908a6017a3c57fe002f51"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f51/4bc908a6017a3c57fe002f51/fight-club-poster.jpg" size="poster" id="4bc908a6017a3c57fe002f51"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f51/4bc908a6017a3c57fe002f51/fight-club-thumb.jpg" size="thumb" id="4bc908a6017a3c57fe002f51"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f4d/4bc908a5017a3c57fe002f4d/fight-club-original.jpg" size="original" id="4bc908a5017a3c57fe002f4d"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f4d/4bc908a5017a3c57fe002f4d/fight-club-poster.jpg" size="poster" id="4bc908a5017a3c57fe002f4d"/> 
     <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/f4d/4bc908a5017a3c57fe002f4d/fight-club-thumb.jpg" size="thumb" id="4bc908a5017a3c57fe002f4d"/> 
     </images> 
     <version>8</version> 
     <last_modified_at>2010-04-26 23:08:53</last_modified_at> 
    </movie> 
    </movies> 
</OpenSearchDescription> 
+0

你有一个xml的例子吗?你打算如何处理属性? – 2012-03-12 10:48:37

+0

已添加文件.... – 2012-03-14 06:40:00

+0

您认为输出看起来像什么?尤其是考虑到opensearch的属性:查询,类别和图像元素。 JSON并不是真的有这样的概念。 – mlathe 2012-03-14 16:43:46

回答

0

如果您已经使用Grails,您可以看看Grails的转换器。

Grails Converters

他们提供消费API XML,并把它们转换成JSON的快捷方式。