将XmlSlurper GPathResult转换为JSON字符串

时间:2012-03-12 10:32:42

标签: json grails groovy xml-serialization

做了很多谷歌搜索.....

是否有将XMLSlurper结果(GPathResult)转换为JSON的“简单方法”?

我添加了一个示例XML文件,我想要获取服务器端,将其存储在DB(JSON)中并转储到浏览器页面以进行javascript处理......

谢谢!

桑德

请参阅以下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>

1 个答案:

答案 0 :(得分:0)

如果您已经在使用Grails,那么您可以查看Grails转换器。

Grails Converters

它们提供了一种快速使用api XML并将其转换为JSON的方法。