Mojo::XMLRPC::Message::Response 转换为 Mojolicious JSON

huangapple go评论95阅读模式
英文:

Mojo::XMLRPC::Message::Response to Mojolicious JSON

问题

我有一个XML-RPC响应,但我在使用Mojo::XMLRPC将其转换为JSON时遇到了困难。我感激您的指导。

英文:

I have an XML-RPC response, but I'm struggling to return it as a JSON using Mojo::XMLRPC. I appreciate your guidance.

  1. use Mojolicious::Lite;
  2. use Mojo::XMLRPC qw[decode_xmlrpc];
  3. #use XMLRPC::Fast;
  4. get '/' => sub {
  5. my $c = shift;
  6. my $xml = <<~'XML';
  7. <?xml version="1.0" encoding="utf-8"?>
  8. <methodResponse>
  9. <params>
  10. <param>
  11. <value>
  12. <struct>
  13. <member>
  14. <name>myarray</name>
  15. <value>
  16. <array>
  17. <data>
  18. <value>
  19. <struct>
  20. <member>
  21. <name>bob</name>
  22. <value>
  23. <boolean>0</boolean>
  24. </value>
  25. </member>
  26. <member>
  27. <name>alice</name>
  28. <value>
  29. <i4>1</i4>
  30. </value>
  31. </member>
  32. </struct>
  33. </value>
  34. </data>
  35. </array>
  36. </value>
  37. </member>
  38. </struct>
  39. </value>
  40. </param>
  41. </params>
  42. </methodResponse>
  43. XML
  44. $c->render(json => decode_xmlrpc($xml));
  45. };
  46. app->start;

答案1

得分: 1

根据文档 https://metacpan.org/pod/Mojo::XMLRPCdecode_xmlrpc()from_xmlrpc() 方法返回一个包含结果的 Mojo::XMLRPC::Message 对象。查看该模块的源代码 https://metacpan.org/release/JBERGER/Mojo-XMLRPC-0.06/source/lib/Mojo/XMLRPC/Message.pm,显示它有一个 parameters 方法。因此,你可以尝试以下代码:

  1. $c->render(json => decode_xmlrpc($xml)->parameters);
英文:

According to the documentation https://metacpan.org/pod/Mojo::XMLRPC the decode_xmlrpc() and from_xmlrpc() methods return a Mojo::XMLRPC::Message object containing the result. Looking at that module's source code https://metacpan.org/release/JBERGER/Mojo-XMLRPC-0.06/source/lib/Mojo/XMLRPC/Message.pm shows that it has a parameters method. So you could try the following:

  1. $c->render(json => decode_xmlrpc($xml)->parameters);

huangapple
  • 本文由 发表于 2023年6月15日 21:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定