Code: Alles auswählen.
DATA: lt_zinvoice TYPE TABLE OF ty_zinvoice,
lv_json TYPE string.
SELECT invoice_ID , paymeth, price, currency FROM ZINVOICE_TAB_2 INTO TABLE @lt_zinvoice UP TO 1 ROWS.
/ui2/cl_json=>serialize(
EXPORTING
data = lt_zinvoice
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
RECEIVING
r_json = lv_json ).
WRITE / lv_json.
WRITE / lv_json.
*send JSON away
" Create client
cl_http_client=>create_by_url(
EXPORTING
url = 'https://xxxxxxx.xxxxxxxxx'
IMPORTING
client = DATA(lo_client)
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
" Prepare request
lo_client->request->set_method( if_http_request=>co_request_method_post ).
lo_client->request->set_form_field( lv_json ).
Code: Alles auswählen.
lo_client->request->set_data( ... ).
Folgende Benutzer bedankten sich beim Autor a-dead-trousers für den Beitrag:
sap_koun
Code: Alles auswählen.
http_status_code: 415
status_text: Unsupported Media Type
Code: Alles auswählen.
o_client->request->set_header_field( name = 'Content-Type' value = 'application/json' )
Code: Alles auswählen.
unprocessable entity
Code: Alles auswählen.
o_client->request->set_header_field( name = 'Content-Type' value = 'application/json' )
Code: Alles auswählen.
o_client->request->set_content_type( content_type = 'application/json' )
Code: Alles auswählen.
lo_client->request->set_form_field( lv_json ).
Code: Alles auswählen.
http_status_code: 422
status_text: Unprocessable Entity
Code: Alles auswählen.
DATA: lt_zinvoice TYPE TABLE OF ty_zinvoice,
lv_json TYPE xstring.
...
* set http method
o_client->request->set_method( if_http_request=>co_request_method_post ).
o_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
o_client->request->set_data( lv_json ).
Code: Alles auswählen.
lo_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
Code: Alles auswählen.
o_client->receive( ).
Data: lv_http_status type i,
lv_status_text type string.
o_client->response->get_status( IMPORTING
code = lv_http_status
reason = lv_status_text ).
write: / 'http_status_code:', lv_http_status.
write: / 'status_text:', lv_status_text.
if lv_http_status = 200.
Data(lv_result) = o_client->response->get_cdata( ).
write: / 'Response:'.
write: / lv_result.
endif.
* close http connection
o_client->close( ).
endif.
CATCH cx_root INTO DATA(e_txt).
WRITE: / e_txt->get_text( ).
ENDTRY.
message 'Your ID is' && *variable für ID*? type 'i'.
Code: Alles auswählen.
...
if lv_http_status = 200.
Data(lv_result) = o_client->response->get_cdata( ).
write: / 'Response:'.
write: / lv_result.
DATA(json) = o_client->response->get_cdata( ).
...
endif.
Code: Alles auswählen.
DATA gv_json_output TYPE string.
SELECT carrid, connid, fldate, price
FROM sflight
INTO TABLE @DATA(it_sflight)
UP TO 4 ROWS.
**JSON converter class - Method -> Serialize method to convert data in JSON
gv_json_output =
/ui2/cl_json=>serialize(
data = it_sflight
compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
**Now check JSON output converted format
cl_demo_output=>display( gv_json_output ).
TYPES: BEGIN OF str_sflight_input,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
END OF str_sflight_input.
DATA itb_sflight_input TYPE STANDARD TABLE OF str_sflight_input.
CLEAR itb_sflight_input[].
**JSON converter class - Method -> Deserialize convert JSON string data into internal table
/ui2/cl_json=>deserialize(
EXPORTING json = gv_json_output
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING data = itb_sflight_input ).
**Internal table filled from JSON input data
cl_demo_output=>display( itb_sflight_input ).
*SELECT itb_sflight_input~carrid, itb_sflight_input~connid, itb_sflight_input~price
* FROM @itb_sflight_input AS itb_sflight_input INTO message.
"
Data(carrid) = carrid type string.
Data(lv_number) = carrid.
Data(lv_string) = 'Test'.
message I000(Z_MESSAGE_BALANCE) with lv_number lv_string.
Code: Alles auswählen.
"oder dein präferierter Reader...
DATA(lo_reader) = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( lv_dein_json ) ).
DO.
DATA(lo_node) = lo_reader->read_next_node( ).
IF lo_node IS INITIAL.
EXIT.
ENDIF.
IF lo_node->type = if_sxml_node=>co_nt_element_open.
"JSON-Objektbezeichner sind als Attribut 'name' dargestellt
DATA(lo_value) = CAST if_sxml_open_element( lo_node )->get_attribute_value( `name` ).
"Bezeichner des Keys
IF lo_value IS BOUND AND lo_value->get_value( ) = `id`.
DATA(lo_value_node) = CAST if_sxml_value( lo_reader->read_next_node( ) ).
"der Wert zum Key
DATA(lv_id_value) = lo_value_node->get_value( ).
EXIT.
ENDIF.
ENDIF.
ENDDO.
Code: Alles auswählen.
message I000(Z_MESSAGE_TEST) with lv_id_value.
Code: Alles auswählen.
DATA wa TYPE spfli.
SELECT *ein bestimmtes Feld in einer Tabelle anvisieren und in einen String umwandeln zur Ausgabe*.
READ TABLE str_sflight_input
Data(carrid) = carrid type string.
Data(lv_number) = carrid.
Data(lv_string) = 'Test'.
message I000(Z_MESSAGE_BALANCE) with lv_number lv_string.
Code: Alles auswählen.
Quelltyp \Class=CL_SXML-OPEN_ELEMENT ist nicht zuweisungskompatibel zu Zieltyp \INTERFACE=IF_SXML_VALUE."