json - 将XmlSlurper GPathResult转换为JSON字符串

标签 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>

最佳答案

如果您已经在使用Grails,则可以看看Grails Converters。

Grails Converters

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

关于json - 将XmlSlurper GPathResult转换为JSON字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665435/

相关文章:

java - 使用 Jackson 的动态字段名称

jquery - 使用 jQuery 创建和附加数据 block

ajax - 关于Grails中的Ajax的问题

mysql - 在 grails 中保存 2 个类的值

javascript - 递归循环 JSON 数据

php - PHP 中的 MySQL 到 JSON

grails - 可以让 Controller 两次将文本值呈现给客户端

grails - 无法访问Spring Security UI?

groovy - 获取SoapUI-project中的所有测试用例和测试步骤

regex - 如何检查 Jenkins 婴儿车是否包含字符