2012-03-12 62 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的快捷方式。