json_to_http_fields

in src/zadf/zcl_adf_service.clas.abap [0:0]


  METHOD json_to_http_fields.
    DATA: ls_fields       TYPE ihttpnvp,
          l_node          TYPE REF TO if_sxml_node,
          l_error         TYPE string,
          lr_open_element TYPE REF TO  if_sxml_open_element,
          lt_attributes   TYPE if_sxml_attribute=>attributes,
          ls_attribute    LIKE LINE OF lt_attributes,
          lr_value_node   TYPE REF TO if_sxml_value_node,
          json            TYPE xstring,
          reader          TYPE REF TO if_sxml_reader,
          parse_error     TYPE REF TO cx_sxml_parse_error.
    TRY .
        json = cl_abap_codepage=>convert_to( iv_response_data ).
        reader = cl_sxml_string_reader=>create( json ).
        " after parse, the json response should look like following

        DO.
          CLEAR ls_fields.
          l_node = reader->read_next_node( ).

          IF l_node IS INITIAL.
            EXIT.
          ENDIF.
          CASE l_node->type.
            WHEN if_sxml_node=>co_nt_element_open.
              lr_open_element ?= l_node.
              lt_attributes  = lr_open_element->get_attributes( ).
              IF lt_attributes IS NOT INITIAL.
                " get name
                READ TABLE lt_attributes INDEX 1 INTO ls_attribute.
                ls_fields-name = ls_attribute->get_value( ).
                " get value
                l_node = reader->read_next_node( ).
                IF l_node->type = if_sxml_node=>co_nt_value.
                  lr_value_node ?= l_node .
                  ls_fields-value = lr_value_node->get_value( ).
                  " add field into the result table
                  APPEND ls_fields TO et_fields.
                ENDIF.
              ENDIF.
            WHEN OTHERS.
              " do nothing
          ENDCASE.
        ENDDO.
      CATCH cx_sxml_parse_error INTO parse_error.
        RAISE EXCEPTION TYPE zcx_adf_service
          EXPORTING
            textid       = zcx_adf_service=>parse_error
            interface_id = gv_interface_id.
    ENDTRY.