abap json转内表
时间: 2023-12-09 13:36:01 浏览: 136
ABAP可以使用类`/UI2/CL_JSON`中的方法将JSON字符串转换为内表。具体步骤如下:
1. 定义内表类型和JSON字符串变量。
```ABAP
TYPES: BEGIN OF T_RECORD1,
NUMNO TYPE CHAR10,
PRODNAME TYPE CHAR50,
END OF T_RECORD1.
DATA: JSON1 TYPE STRING.
DATA: RECORD1 TYPE TABLE OF T_RECORD1 WITH HEADER LINE.
```
2. 将JSON字符串赋值给变量`JSON1`。
```ABAP
JSON1 = '[{"NumNo":"001","ProdName":"Fert01"},{"NumNo":"002","ProdName":"Fert02"}]'.
```
3. 调用`/UI2/CL_JSON=>DESERIALIZE`方法将JSON字符串转换为内表。
```ABAP
/UI2/CL_JSON=>DESERIALIZE(
EXPORTING
JSON = JSON1
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING
DATA = RECORD1[]
).
```
4. 遍历内表并输出结果。
```ABAP
LOOP AT RECORD1.
WRITE : / RECORD1-NUMNO, RECORD1-PRODNAME.
ENDLOOP.
```
完整代码如下:
```ABAP
TYPES: BEGIN OF T_RECORD1,
NUMNO TYPE CHAR10,
PRODNAME TYPE CHAR50,
END OF T_RECORD1.
DATA: JSON1 TYPE STRING.
DATA: RECORD1 TYPE TABLE OF T_RECORD1 WITH HEADER LINE.
JSON1 = '[{"NumNo":"001","ProdName":"Fert01"},{"NumNo":"002","ProdName":"Fert02"}]'.
/UI2/CL_JSON=>DESERIALIZE(
EXPORTING
JSON = JSON1
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING
DATA = RECORD1[]
).
LOOP AT RECORD1.
WRITE : / RECORD1-NUMNO, RECORD1-PRODNAME.
ENDLOOP.
```
阅读全文