CPI:Groovy空指针异常[JsonSlurper]

2020-09-01 16:46发布

点击此处---> 群内免费提供SAP练习系统(在群公告中)加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)嗨 我具有以下常规脚本来从...

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

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


我具有以下常规脚本来从JSON读取下一页值,但是,当未找到'nextPage'时,会遇到nullpointer运行时异常:

尝试添加catch块,但问题仍然存在。 有任何帮助请解决吗?

常规脚本

导入com.sap.gateway.ip.core.customdev.util.Message;
 导入java.util.HashMap;
 导入groovy.json.JsonSlurper;
 def消息processData(消息消息){
    //身体
     尝试{
         def主体= message.getBody();
         def map = message.getProperties();
     
         def parsedJson =新的JsonSlurper()。parseText(body)
         def val = parsedJson?.nextPage [0];
     
         message.setProperty(" nextPage",val); 返回消息;
     } catch(NullPointerException e){
         message.setProperty(" nextPage",""); 返回消息;
     }}
 

JSON输入:

 [
   {
     "数据":{
       " isDataTrue":是的,
       "总计":[
         {
           "值":[
             " 30889.63",
             " 305.8379207920792"
           ]
         }
       ],
       " rowCount":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",
           ]
         }
       ]
     },
     " columnHeader":{
       " metricHeader":{
         " metricHeaderEntries":[
           {
             " name":" 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:分组"
       ]
     },
     " nextPage":" 32000"
   }
 ]
 

当在JSON中找到" nextPage"时,脚本将起作用,否则将记录运行时异常。

capture.jpg (40.9 kB)
5条回答
葫芦娃快救爷爷
2020-09-01 17:04

Hello Paaps,

有很多可用的编辑器,例如intelliJ,eclipse(请浏览我们的SAP社区,也有很好的博客)。

我个人而言,我使用groovy sdk控制台 测试小代码,然后在SAP CPI中使用相同的代码。 以下是下载链接。

http://groovy-lang.org/download.html

问候,

Sriprasad Shivaram Bhat

一周热门 更多>