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

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

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.

use Mojolicious::Lite;
use Mojo::XMLRPC qw[decode_xmlrpc];
#use XMLRPC::Fast;

get '/' => sub {
    my $c = shift;

    my $xml = <<~'XML';
    <?xml version="1.0" encoding="utf-8"?>
    <methodResponse>
      <params>
        <param>
          <value>
            <struct>
              <member>
                <name>myarray</name>
                <value>
                  <array>
                    <data>
                      <value>
                        <struct>
                          <member>
                            <name>bob</name>
                            <value>
                              <boolean>0</boolean>
                            </value>
                          </member>
                          <member>
                            <name>alice</name>
                            <value>
                              <i4>1</i4>
                            </value>
                          </member>
                        </struct>
                      </value>
                    </data>
                  </array>
                </value>
              </member>
            </struct>
          </value>
        </param>
      </params>
    </methodResponse>
    XML
    
    $c->render(json => decode_xmlrpc($xml));
};

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 方法。因此,你可以尝试以下代码:

$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:

$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:

确定