如何将json数组转换为xml数组以用于发布请求?

时间:2017-08-09 12:03:33

标签: arrays json xml

我有一个JSON请求的以下帖子数据正常工作。

  

“customer”=> {“subdomain”=>“Test”,“firstname”=>“john”,   “lastname”=>“doe”,“email”=>“john.doe@example.com”,   “company”=>“Sample”,“default_language”=>“en”,   “active_modules”=> [“cmdb”,“evm”,“itil”]}

我还为我的服务器启用了XML,因此也希望响应XML post请求。我试图将上面的JSON数据转换为XML,但它并不像预期的那样。

<customer>
   <active_modules>
      <element>cmdb</element>
      <element>evm</element>
      <element>itil</element>
   </active_modules>
   <company>Sample</company>
   <default_language>en</default_language>
   <email>john.doe@example.com</email>
   <firstname>john</firstname>
   <lastname>doe</lastname>
   <subdomain>Test</subdomain>
</customer>

问题在于数组元素。如何转换此数组元素以将与JSON请求完全相同的数据传递给服务器?

1 个答案:

答案 0 :(得分:0)

对于数组,您需要多次传递同一节点。

<customer>
   <active_modules>cmdb</active_modules>
   <active_modules>evm</active_modules>
   <active_modules>itil</active_modules>
   <company>Sample</company>
   <default_language>en</default_language>
   <email>john.doe@example.com</email>
   <firstname>john</firstname>
   <lastname>doe</lastname>
   <subdomain>Test</subdomain>
</customer>
相关问题