This ABAP report is included in the addon package.
&---------------------------------------------------------------------
& Report /CEX/JMP_JSON_EXAMPLE
&---------------------------------------------------------------------
& This is example code to show the usage of JSON within ABAP
& Build your JSON object, create a JSON String and parse it back
&---------------------------------------------------------------------
& (c) 2014 joomp.de
& last changed 2014/02/05
&---------------------------------------------------------------------*
REPORT /cex/jmp_json_example NO STANDARD PAGE HEADING LINE-SIZE 1023.
-------- interface
PARAMETERS: p_dec TYPE rsdecimals DEFAULT 3.
-------- local data
DATA: lr_json TYPE REF TO /cex/cl_jmp_json.
DATA: lr_output TYPE REF TO /cex/cl_jmp_json_object.
DATA: lr_object TYPE REF TO /cex/cl_jmp_json_object.
DATA: lr_array TYPE REF TO /cex/cl_jmp_json_array.
DATA: lr_data TYPE REF TO /cex/if_jmp_json.
DATA: lr_parsed TYPE REF TO /cex/if_jmp_json.
DATA: lr_input TYPE REF TO /cex/cl_jmp_json_object.
DATA: lv_type TYPE /cex/jmp_json_data_type.
DATA: lv_double TYPE /cex/jmp_json_double.
DATA: lv_output TYPE string.
DATA: lv_data TYPE string.
DATA: lv_is_null TYPE abap_bool.
DATA: lv_len TYPE i.
DATA: lv_index TYPE i.
START-OF-SELECTION.
-------- 0) init
/cex/cl_jmp_json=>set_decimals( p_dec ).
lv_double = '1234.876432'.
lv_data = 'my litte string with chars to "escape" and special chars äöß'.
CONCATENATE lv_data '-' sy-uname INTO lv_data SEPARATED BY ' '.
-------- 1) get the handler
lr_json = /cex/cl_jmp_json=>get_instance( ).
-------- 2) get a new json object as value holder
lr_output = lr_json->new_object( ).
-------- 3) add some data to the main json object
lr_output->put_string( iv_id = 'string' iv_data = lv_data ).
lr_output->put_boolean( iv_id = 'boolean' iv_data = abap_true ).
lr_output->put_integer( iv_id = 'integer' iv_data = 45 ).
lr_output->put_double( iv_id = 'double' iv_data = lv_double ).
-------- 4) create an array and fill it with integer values
lr_array = lr_json->new_array( ).
DO 20 TIMES.
lr_array->put_integer( sy-index ).
ENDDO.
lr_output->put( iv_id = 'array_integer' ir_data = lr_array ).
-------- 5) create a subobject with an integer array
lr_output->put( iv_id = 'subobject' ir_data = lr_object ).
-------- 6) get the json output as string
lv_output = lr_output->/cex/if_jmp_json~to_string( ).
WRITE: / lv_output.
-------- 7) get the content of the output object as keys
lr_array = lr_output->names( ).
lv_len = lr_array->length( ).
lv_index = 0.
DO lv_len TIMES.
lr_data = lr_array->get( lv_index ).
lv_type = lr_data->get_data_type( ).
lv_is_null = lr_data->is_null( ).
lv_data = lr_data->to_string( ).
WRITE: / lv_index, lv_type, lv_is_null, lv_data. ADD 1 TO lv_index.
ENDDO.