ABAP内表数据和JSON格式互转
本程序演示ABAP内表数据如何转为JSON格式,以及JSON数据如何放入内表。
注:json字符串格式如:jsonstr = '[ {flag: "0",message: "abc"},{flag: "1",message: "abcdddd"}]'.
另注意转换key value双引号问题
*&---------------------------------------------------------------------* *& Report Z_YHY_TEST_JSON *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT Z_YHY_TEST_JSON. DATA: json_ser TYPE REF TO CL_TREX_JSON_SERIALIZER, json_des TYPE REF TO CL_TREX_JSON_DESERIALIZER. DATA: jsonstr TYPE string. DATA: BEGIN OF itab OCCURS 0, matnr LIKE makt-matnr, maktx LIKE makt-maktx, END OF itab. START-OF-SELECTION. SELECT * INTO CORRESPONDING FIELDS OF TABLE itab FROM makt UP TO 10 ROWS. ***内表->JSON CREATE OBJECT json_ser EXPORTING data = itab[]. CALL METHOD json_ser->SERIALIZE. CALL METHOD json_ser->GET_DATA RECEIVING rval = jsonstr. WRITE: jsonstr. ***JSON->内表 CREATE OBJECT json_des. CALL METHOD json_des->DESERIALIZE EXPORTING json = jsonstr IMPORTING abap = itab[].