Convert database query result to json/yaml/xml with ABAP

。_饼干妹妹 提交于 2019-12-11 09:02:44

问题


Is there a way to convert a database query result to json/yaml/xml with ABAP?

I am new to SAP software development.


回答1:


    DATA: lt_flight TYPE STANDARD TABLE OF sflight,
          lrf_descr TYPE REF TO cl_abap_typedescr,
          lv_json   TYPE string.

        SELECT * FROM sflight INTO TABLE lt_flight.

        * serialize table lt_flight into JSON, skipping initial fields and converting ABAP field names into camelCase
        lv_json = /ui2/cl_json=>serialize( data = lt_flight compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
        WRITE / lv_json.

        CLEAR lt_flight.

* deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping
/ui2/cl_json=>deserialize( EXPORTING json = lv_json pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = lt_flight ).

* serialize ABAP object into JSON string
lrf_descr = cl_abap_typedescr=>describe_by_data( lt_flight ).
lv_json = /ui2/cl_json=>serialize( lrf_descr ).
WRITE / lv_json.

Output

[{"mandt":"000","carrid":"AA","connid":0017,"fldate":20130515,"price":422.94,"currency":"USD","planetype":"747-400","seatsmax":385,
"seatsocc":375,"paymentsum":192683.30,"seatsmaxB":31,"seatsoccB":31,"seatsmaxF":21,"seatsoccF":19},{"mandt":"000","carrid":"AA",
"connid....
{"ABSOLUTE_NAME":"\\TYPE=%_T00004S00000000O0000012480","ADMIN_TAB":"\\TYPE=%_T00004S00000000O0000012480",
"ADMIN_TAB_LINE":"\\TYPE=%_T00004S00000000O0000012480","DECIMALS":0,"HAS_UNIQUE_KEY":false,"INITIAL_SIZE":0,
"KEY":[{"NAME":"MANDT"},{"NAME":"CARRID"},{"NAME":"CO....

for more information please read Serializer and Deserializer

UPDATE

Another example:

First, you should create the ABAP to JSON Writer with the CL_SXML_STRING_WRITER with Json as Type. Then Call the transformation to convert the String to JSON formatted String . The last Step is to retrieve the Output of the Writer in JSON.

DATA text TYPE string VALUE `Hello JSON, I’m ABAP!`.
DATA writer TYPE REF TO cl_sxml_string_writer.
DATA json TYPE xstring.

    “ABAP to JSON
    writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
    CALL TRANSFORMATION id SOURCE text = text
                            RESULT XML writer.
    json = writer->get_output( ).


来源:https://stackoverflow.com/questions/51611319/convert-database-query-result-to-json-yaml-xml-with-abap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!