核心:/UI2/CL_JSON=>SERIALIZE
和/UI2/CL_JSON=>DESERIALIZE
一个json数组和abap内表的转换
REPORT ZDCH_09_TEST2.
"一个json数组和abap内表的转换
DATA: LT_FLIGHT TYPE STANDARD TABLE OF SFLIGHT,
LV_JSON TYPE STRING.
SELECT * FROM SFLIGHT INTO TABLE LT_FLIGHT.
* ABAP内表转JSON
LV_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LT_FLIGHT COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LV_JSON ).
CLEAR LT_FLIGHT.
* JSON转ABAP内表
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_JSON PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE CHANGING DATA = LT_FLIGHT ).
CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_FLIGHT ).
结果:
LV_JSON
用工具美化一下:
LT_FLIGHT:
一条json数据和abap结构之间的转换
"一条json数据和abap结构之间的转换
DATA: LW_FLIGHT LIKE SFLIGHT,
LV_JSON TYPE STRING.
SELECT SINGLE
*
FROM SFLIGHT INTO LW_FLIGHT.
* ABAP内表转JSON
LV_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LW_FLIGHT COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LV_JSON ).
CLEAR LW_FLIGHT.
* JSON转ABAP
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_JSON PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE CHANGING DATA = LW_FLIGHT ).
CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_FLIGHT ).
结果:
LV_JSON
美化一下:
LW_FLIGHT: