ABAP接口程序开发中时常会用到JSON格式来传输数据,在监控传输的JSON串内容时,把JSON转换为HTML格式来显示会很便利。
下面提供一个简单例子来实现JSON转化为HTML并显示的功能。
其中:
1,使用 /ui2/cl_json=>serialize来生成JSON串,
2,调用 TRANSFORMATION sjson2html来转换HTML,
3,使用 cl_abap_browser=>show_html显示HTML。
代码:
REPORT ztest_display_json. DATA:lv_json TYPE string, lv_convert TYPE string, lo_json_ser TYPE REF TO cl_trex_json_serializer, lv_err_text TYPE string, lt_ekpo TYPE STANDARD TABLE OF ekpo. SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_ekpo FROM ekpo UP TO 10 ROWS. * 将内表转化为JSON lv_json = /ui2/cl_json=>serialize( data = lt_ekpo[] ). TRY. * 将JSON转换为HTML CALL TRANSFORMATION sjson2html SOURCE XML lv_json RESULT XML DATA(lv_html). CATCH cx_xslt_runtime_error INTO DATA(lo_err). lv_err_text = lo_err->get_text( ). WRITE: lv_err_text. RETURN. ENDTRY. * 显示HTML lv_convert = cl_abap_codepage=>convert_from( lv_html ). cl_abap_browser=>show_html( html_string = lv_convert ).
运行后弹出一个窗口显示HTML也可以通过‘+’‘—’来收起或展开JSON,很方便。
<img class="alignnone size-medium wp-image-6470" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHAvd3d3LmJhaWR1c2FwLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxOS0wNi0xMl8xNi0xOC00Ni03NDB4NDc1LmpwZw==.jpg" width="740" height="475" srcset="http://www.baidusap.com/wp-content/uploads/2019-06-12_16-18-46-740x475.jpg 740w, http://www.baidusap.com/wp-content/uploads/2019-06-12_16-18-46-768x493.jpg 768w, http://www.baidusap.com/wp-content/uploads/2019-06-12_16-18-46-1024x657.jpg 1024w, http://www.baidusap.com/wp-content/uploads/2019-06-12_16-18-46.jpg 1078w" sizes="(max-width: 740px) 100vw, 740px" />
如果不想把HTML显示在窗口中,可以利用一下show_html中的参数CONTAINER,将HTML显示在自己画的屏幕中。
<img class="alignnone size-medium wp-image-6471" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHAvd3d3LmJhaWR1c2FwLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxOS0wNi0xMl8xNi0yMC01OS03NDB4NDYwLmpwZw==.jpg" width="740" height="460" srcset="http://www.baidusap.com/wp-content/uploads/2019-06-12_16-20-59-740x460.jpg 740w, http://www.baidusap.com/wp-content/uploads/2019-06-12_16-20-59-768x477.jpg 768w, http://www.baidusap.com/wp-content/uploads/2019-06-12_16-20-59.jpg 934w" sizes="(max-width: 740px) 100vw, 740px" />
其他:TRANSFORMATION sjson2html可以通过Tcode STRANS来查看。
<img class="alignnone size-full wp-image-6472" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHAvd3d3LmJhaWR1c2FwLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxOS0wNi0xMl8xNi0yMy0xOC5qcGc=.jpg" width="482" height="213" />
以上。
原文地址:https://www.cnblogs.com/datie/p/11428416.html