之前一篇博客描述了如何在sap abap中调用webapi,详见 SAP ABAP调用WEBAPI(一)
今天写一下关于abap结构或内表如何实现与json字符串的相互转换.
可使用的类有很多,如cl_trex_json_serializer与cl_trex_json_deserializer,SAP也为我们提供了CL_FDT_JSON, /UI2/CL_JSON等来进行JSON的序列化与反序列化。
由于sap的版本问题,可能某些类在服务器上不存在而无法使用。该 博客 作者详细描述了如何用abap代码使用序列化与反序列化,
同时提供了/UI2/CL_JSON的代码,可供 下载 使用。
在此我提供一个将/UI2/CL_JSON代码处理好的include ZBC_UI2_JSON_CLS.txt ,供大家在缺少该类的情况下,添加到主程序中使用。
------------------------------------------------------------------------------------------------------------------------------------------
1、如何使用cl_trex_json_serializer与cl_trex_json_deserializer实现序列化与反序列化。
(我的sap版本中没有类cl_trex_json_deserializer ZBC_CL_TREX_JSON_DESERIALIZER.txt ,include供大家上传使用)
REPORT yaiolos_test_06.
" **********************************************************************
"! Usage examples and documentation can be found on SCN:
" http://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
" ********************************************************************** "
INCLUDE zbc_ui2_json_cls.
INCLUDE zbc_cl_trex_json_deserializer.
DATA: lt_flight TYPE STANDARD TABLE OF sflight WITH HEADER LINE,
lv_json TYPE string,
lv_json_out TYPE string,
lv_json_len TYPE i.
DATA:
serializer TYPE REF TO cl_trex_json_serializer,
deserializer TYPE REF TO zcl_trex_json_deserializer.
START-OF-SELECTION.
***序列化
**********************************************************************
*Usage examples and documentation can be found on https://www.cnblogs.com/hhelibeb/p/6617488.html
**********************************************************************
SELECT * FROM sflight INTO TABLE lt_flight.
CREATE OBJECT serializer
EXPORTING
DATA = lt_flight[].
serializer->serialize( ).
lv_json = serializer->get_data( ).
lv_json_out = lv_json.
DO.
lv_json_len = strlen( lv_json_out ).
IF lv_json_len < 100.
WRITE / lv_json_out.
EXIT.
ELSE.
WRITE / lv_json_out+100.
ENDIF.
lv_json_out = lv_json_out+100.
ENDDO.
***反序列化
REFRESH lt_flight.
CREATE OBJECT deserializer.
deserializer->deserialize(
EXPORTING json = lv_json
IMPORTING abap = lt_flight[] ).
LOOP AT lt_flight.
WRITE /: lt_flight-carrid.
ENDLOOP.
2、使用 /ui2/cl_json来序列化和反序列化json字符串。
REPORT yaiolos_test_07.
" **********************************************************************
"! Usage examples and documentation can be found on SCN:
" http://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
" ********************************************************************** "
INCLUDE zbc_ui2_json_cls.
INCLUDE zbc_cl_trex_json_deserializer.
DATA: lt_flight TYPE STANDARD TABLE OF sflight,
ls_flight TYPE sflight,
lrf_descr TYPE REF TO cl_abap_typedescr,
lv_json TYPE string.
DATA : lv_json_len TYPE i,
lv_json_out TYPE string.
DEFINE macro_write.
lv_json_out = &1.
do.
lv_json_len = strlen( lv_json_out ).
if lv_json_len < 100.
write / lv_json_out.
exit.
else.
write / lv_json_out+100.
endif.
lv_json_out = lv_json_out+100.
enddo.
END-OF-DEFINITION.
START-OF-SELECTION.
SELECT * FROM sflight INTO TABLE lt_flight.
* serialize table lt_flight into JSON, skipping initial fields and converting ABAP field names into camelCase
lv_json = zui2_json=>serialize( data = lt_flight compress = abap_true pretty_name = zui2_json=>pretty_mode-camel_case ).
macro_write lv_json.
CLEAR lt_flight.
* deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping
zui2_json=>deserialize( EXPORTING json = lv_json pretty_name = zui2_json=>pretty_mode-camel_case CHANGING data = lt_flight ).
LOOP AT lt_flight INTO ls_flight.
WRITE / ls_flight-carrid.
ENDLOOP.
与1转化的不同点在于字段名是否有"号包含,1的方式使用标准json格式无法解析,2更通用。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7714329/viewspace-2646458/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7714329/viewspace-2646458/