eclipse 在abap中将JSON转换为CSV

p3rjfoxz  于 2022-11-04  发布在  Eclipse
关注(0)|答案(1)|浏览(201)

我需要以下JSON到csv在abap adt。

data(rep) = {
        "name": "John",
        "age": "22",
        "gender": "male",

    }
    {
        "name": "ram",
        "age": "21",
        "gender": "male",

    }
    {
        "name": "Janu",
        "age": "22",
        "gender": "female",

    }

该函数用于转换CSV。

xnifntxz

xnifntxz1#

我建议您首先将JSON字符串保存到一个内部表中,然后从内部表中创建一个CSV。

TYPES: BEGIN OF t_person,
         name  TYPE string,
         age TYPE i,
         gender TYPE string,
       END OF t_person.

TYPES: tt_person TYPE STANDARD TABLE OF t_person WITH DEFAULT KEY.

DATA: lt_csv TYPE truxs_t_text_data.

DATA: json TYPE string VALUE '{"VALUES":[{
        "name": "John",
        "age": "22",
        "gender": "male",

    },
    {
        "name": "ram",
        "age": "21",
        "gender": "male",
    },
    {
        "name": "Janu",
        "age": "22",
        "gender": "female",
    }]}'.

DATA(lt_persons) = VALUE tt_person( ).

"Convert to internal table
CALL TRANSFORMATION id SOURCE XML json RESULT values = lt_persons.

"Convert to CSV
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
    EXPORTING
      i_field_seperator    = ';'
    TABLES
      i_tab_sap_data       = lt_persons
    CHANGING
      i_tab_converted_data = lt_csv
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.

相关问题