[CPI]使用Groovy将JSON转换为XML

2020-09-01 13:27发布

         点击此处--->   EasySAP.com群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


考虑到需要映射到接收器的数据结构,我需要使用一个常规脚本从JSON创建xml。 尽管某些部分有效,但列表中值的循环似乎是一个问题。

常规脚本下面:

导入groovy.json.JsonSlurper

 def body ='[{" data":{" isDataTrue":true," totals":[{" values":[" 30889.63"," 305.8379207920792"]}]," rowCount":39,"最小":[  {" values":[" 1"," 0.0"]}]," rows":[{" dimensions":[" Visitor"," Organic"," www.compass.com/en/my-account/addresses  "]," metrics":[{" values":[" 1"," 1"," 1"," 0.0"," 0.0"]}]},},{"维度":["访问者","非 -有机"," www.compass.com/en/my-account/addresses"],"指标":[{"值":[" 1"," 0"," 1"," 3.0"," 0.0"  ""]}]}]," maximums":[{" values":[" 5082"," 5080"]}]}," columnHeader":{" metricHeader":{" metricHeaderEntries":[{"名称":  " ga:sessions"," type":" INTEGER"},{" name":" ga:users"," type":" INTEGER"},{" name":" ga:pageviews"," type":  " INTEGER"},{" name":" ga:entrances"," type":" INTEGER"},{" name":" ga:bounceRate"," type":" PERCENT"}]},"尺寸"  :[" ga:userType"," ga:cat"," ga:Grouping"]}," nextPage":" 32000"}]'


     def msg = body.substring(1,body.length()-1);
     def list = new JsonSlurper()。parseText(msg);
      
     println(清单)

 如果(list.data!= null){
 def val = list.data;
 字符串val_str = val.rows;
          字符串prnt = val_str.substring(1,val_str.length()-1);
          println(prnt);
          println('---------手动xml生成--------');
          整数i = 1;
          字符串mvxml =''; 字符串rxml =''; 字符串mdata ='';
          字符串rdata ='';
          rxml ='<行>';
          for(def item:val.rows){
              rdata ='<行>'
             
             for(定义指标:item.metrics.values){
                  mvxml ='';
                  mvxml = mvxml +指标[i] +'';
                  mdata = mdata + mvxml;
                  i = i + 1;
              }
              rdata = rdata + mdata +'';
          }
         如果(i> 1){
            rxml = rxml + rdata +'';
            println(rxml);
         }
     }
 其他{

         
     }
 

我得到以下xml:

 <行>
  <行>
    1 
    1 
  
  

但是我想要拥有什么:

 <行>
  <行>
    1 
    1 
    1 
    0.0 
    0.0 
  
  <行>
    1 
    0 
    1 
    3.0 
    0.0 
  
  

不确定如何读取元素指标中的列表值。 请问有什么帮助吗? 或有关如何处理的建议?

         点击此处--->   EasySAP.com群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


考虑到需要映射到接收器的数据结构,我需要使用一个常规脚本从JSON创建xml。 尽管某些部分有效,但列表中值的循环似乎是一个问题。

常规脚本下面:

导入groovy.json.JsonSlurper

 def body ='[{" data":{" isDataTrue":true," totals":[{" values":[" 30889.63"," 305.8379207920792"]}]," rowCount":39,"最小":[  {" values":[" 1"," 0.0"]}]," rows":[{" dimensions":[" Visitor"," Organic"," www.compass.com/en/my-account/addresses  "]," metrics":[{" values":[" 1"," 1"," 1"," 0.0"," 0.0"]}]},},{"维度":["访问者","非 -有机"," www.compass.com/en/my-account/addresses"],"指标":[{"值":[" 1"," 0"," 1"," 3.0"," 0.0"  ""]}]}]," maximums":[{" values":[" 5082"," 5080"]}]}," columnHeader":{" metricHeader":{" metricHeaderEntries":[{"名称":  " ga:sessions"," type":" INTEGER"},{" name":" ga:users"," type":" INTEGER"},{" name":" ga:pageviews"," type":  " INTEGER"},{" name":" ga:entrances"," type":" INTEGER"},{" name":" ga:bounceRate"," type":" PERCENT"}]},"尺寸"  :[" ga:userType"," ga:cat"," ga:Grouping"]}," nextPage":" 32000"}]'


     def msg = body.substring(1,body.length()-1);
     def list = new JsonSlurper()。parseText(msg);
      
     println(清单)

 如果(list.data!= null){
 def val = list.data;
 字符串val_str = val.rows;
          字符串prnt = val_str.substring(1,val_str.length()-1);
          println(prnt);
          println('---------手动xml生成--------');
          整数i = 1;
          字符串mvxml =''; 字符串rxml =''; 字符串mdata ='';
          字符串rdata ='';
          rxml ='<行>';
          for(def item:val.rows){
              rdata ='<行>'
             
             for(定义指标:item.metrics.values){
                  mvxml ='';
                  mvxml = mvxml +指标[i] +'';
                  mdata = mdata + mvxml;
                  i = i + 1;
              }
              rdata = rdata + mdata +'';
          }
         如果(i> 1){
            rxml = rxml + rdata +'';
            println(rxml);
         }
     }
 其他{

         
     }
 

我得到以下xml:

 <行>
  <行>
    1 
    1 
  
  

但是我想要拥有什么:

 <行>
  <行>
    1 
    1 
    1 
    0.0 
    0.0 
  
  <行>
    1 
    0 
    1 
    3.0 
    0.0 
  
  

不确定如何读取元素指标中的列表值。 请问有什么帮助吗? 或有关如何处理的建议?

付费偷看设置
发送
5条回答
SKY徐
1楼-- · 2020-09-01 13:56

嗨,Papp

您是否有特定原因不使用内置JSON到XML转换器步骤? 转换您的JSON,我得到以下输出:

 <?xml version =" 1.0" encoding =" UTF-8"?>
 <根>
     <数据>
          true 
         <总计>
              30889.63 
              305.8379207920792 
         
          39 
         <最小>
              1 
              0.0 
         
         <行>
             访客
             有机
              www.compass.com/en/my-account/addresses 
             <指标>
                  1 
                  1 
                  1 
                  0.0 
                  0.0 
             
         
         <行>
             访客
             非有机
              www.compass.com/en/my-account/addresses 
             <指标>
                  1 
                  0 
                  1 
                  3.0 
                  0.0 
             
         
         <最大值>
              5082 
              5080 
         
     
     
         
             
                  ga:sessions 
                  INTEGER 
             
             
                  ga:users 
                  INTEGER 
             
             
                  ga:pageviews 
                  INTEGER 
             
             
                  ga:entrances 
                  INTEGER 
             
             
                  ga:bounceRate 
                  PERCENT 
             
         
          ga:userType 
          ga:cat 
          ga:分组
     
      32000 
 

 

要将其映射为所需的格式,请使用以下小型XSLT样式表:

 <?xml version =" 1.0" encoding =" UTF-8"?>
 
     
     
         <行>
             
                 <行>
                     
                         
                             
                         
                     
                 
             
         
     
  

致谢

Morten

Alawn_Xu
2楼-- · 2020-09-01 14:02

我已经用一个小的XSLT样式表更新了答案,该样式表将映射输出 JSON到XML转换器的格式转换为您所需的格式。

问候,

Morten

jovirus
3楼-- · 2020-09-01 13:59

Hello Papps,

是获得输出所需遵循的组合步骤。

步骤1:

内容修饰符来设置有效负载(这可能与您无关)。

步骤2;

从负载中删除[]的脚本(这是为了管理JSON到XML转换器的限制)。

 import com.sap.gateway.ip.core.customdev.util.Message;
 导入java.util.HashMap;
 def消息processData(消息消息){
      //身体
        def主体= message.getBody();
        def body_m = body.substring(1,body.length()-1);
        message.setBody(body_m);
        返回消息;
 }
 

步骤3:

将JSON添加到XML转换器

步骤4:

添加XSLT映射以根据您的要求获取数据( CREDIT转到 Morten Wittrock )。

 <?xml version =" 1.0" encoding =" UTF-8"?>
 
     
     
         <行>
             
                 <行>
                     
                         
                             
                         
                     
                 
             
         
     
 

问候,

Sriprasad Shivaram Bhat

黑丝骑士
4楼-- · 2020-09-01 14:06

Hi Morten

我尝试使用JSON => XML转换但运行时 仍然抛出错误"无法将JSON文档转换为XML:JSON文档不是以'{'开头。"

仍然如此,考虑到JSON模式及其需要映射到的方式并没有太大帮助 target

派大星 ヾ
5楼-- · 2020-09-01 14:03

嗨,Sirprasad

是的,我忘了指出这一点:JSON到XML转换器希望JSON是一个对象,而不是数组(即使是 尽管后者是有效的JSON)。

关于

Morten

一周热门 更多>