ABAP 内表数据 与 Json串 相互转换

 内表:

A B C
IMINGZHA  HAIMINGZ

 AIMINGZH

1 2

3

4 5

6

 

 

 

 

 

 

Json串:  [{a: "IMINGZHA", b: "HAIMINGZ", c: "AIMINGZH"}, {a: "1", b: "2", c: "3"}, {a: "4", b: "5", c: "6"}] 

 

 1   DATA: json_ser TYPE REF TO CL_TREX_JSON_SERIALIZER,
 2             json_des TYPE REF TO CL_TREX_JSON_DESERIALIZER.
 3   DATA: jsonstr TYPE string.
 4 
 5   DATA: BEGIN OF IT_TAB OCCURS 0,
 6             A TYPE CHAR10,
 7             B TYPE CHAR10,
 8             C TYPE CHAR10,
 9         END OF IT_TAB.
10 
11   IT_TAB-A = 'IMINGZHA' .
12   IT_TAB-B = 'HAIMINGZ' .
13   IT_TAB-C = 'AIMINGZH' .
14   APPEND IT_TAB.
15 
16   IT_TAB-A = '1' .
17   IT_TAB-B = '2' .
18   IT_TAB-C = '3' .
19   APPEND IT_TAB.
20 
21   IT_TAB-A = '4' .
22   IT_TAB-B = '5' .
23   IT_TAB-C = '6' .
24   APPEND IT_TAB.
25 
26 *内表 转 Json 串
27   CREATE OBJECT json_ser
28     EXPORTING
29       data = IT_TAB[].
30   CALL METHOD json_ser->SERIALIZE.
31   CALL METHOD json_ser->GET_DATA
32     RECEIVING
33       rval = jsonstr.
34 
35 
36 BREAK-POINT.
37 *Json 串 append 内表 
38   CREATE OBJECT json_des.
39   CALL METHOD json_des->DESERIALIZE
40     EXPORTING
41       json = jsonstr
42     IMPORTING
43       abap = IT_TAB[].
44 
45 
46 BREAK-POINT.

 

posted @ 2017-03-22 18:11  明大叔  阅读(1760)  评论(0编辑  收藏  举报