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

2020-09-01 13:27发布

点击此处---> 群内免费提供SAP练习系统(在群公告中)加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)嗨 考虑到需要映射到接收器...

         点击此处--->   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条回答
黑丝骑士
2020-09-01 14:06

Hi Morten

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

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

一周热门 更多>