¡Caminando hacia el éxito!

Aprende en Comunidad

Avalados por :

Como definir um tipo em ABAP para receber um array de strings como uma tabela

  • Creado 01/03/2024
  • Modificado 01/03/2024
  • 39 Vistas
0
Cargando...

Olá especialistas em ABAP.

Vamos supor que tenho os seguintes dados JSON.

{
	"data": "abc",
	"tab": ["a", "b", "c"]
}<br>

Quero converter isso para uma estrutura ABAP da seguinte forma.

|data |tab   |
|"abc"|3 linhas (primeira: "a", segunda: "b, terceira: "c") |

ou,

|data |tab   |
|"abc"|"a","b","c" |

Inicialmente pensei em definir um tipo e usar /ui2/cl_json=>deserialize como mostrado abaixo.

types: begin of ts_json,
  data type string,
  tab type string, "Isso não está correto!
end of ts_json.


data(json) = '{ "data": "abc", "tab": ["a", "b", "c"] }'.
data ls_data type ts_json.


/ui2/cl_json=>deserialize(
  exporting json = conv #( json )
  changing data = ls_data
).

No entanto, estou preso porque o campo "tab" é uma tabela de strings sem nomes de campo.

Se eu depurar o código, ls_data-data está preenchido, mas ls_data-tab está em branco.

Como posso definir um tipo que possa receber um array de strings como uma tabela?

Pedro Pascal
Se unió el 07/03/2018
Pinterest
Telegram
Linkedin
Whatsapp

4 Respuestas

0
Cargando...

Caro san,

Enfrentei um problema semelhante no ambiente ABAP do BTP.

A pergunta e sua resposta mostraram-se úteis.

O tipo "string_table" também pode ser utilizado, então adicione-o.

  METHOD if_oo_adt_classrun~main.

    DATA:
      begin of ls_result,
        data type string,
*        tab  type standard table of string with empty key,
        tab  TYPE string_table,
      end of ls_result.

    DATA(lv_json_string) = |\{\r\n| &
                           |    "data": "abc",\r\n| &
                           |    "tab": ["a", "b", "c"]\r\n| &
                           |\}|.

    xco_cp_json=>data->from_string( lv_json_string )->apply( VALUE #(
      ( xco_cp_json=>transformation->camel_case_to_underscore )
      ( xco_cp_json=>transformation->boolean_to_abap_bool )
    ) )->write_to( REF #( ls_result ) ).

    out->write( ls_result-data ).
    LOOP AT ls_result-tab REFERENCE INTO DATA(lr).
      out->write( lr->* ).
    ENDLOOP.

  ENDMETHOD.

Resultado.

horitaku_0-1712142408816.png

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Olá sandra.rossi ,

Obrigado por sua resposta rápida. A linha a seguir é o que eu precisava.

tab  TYPE STANDARD TABLE OF string WITH EMPTY KEY,
Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Não respondo exatamente à sua pergunta, mas quando você conhece a Transformação Simples e JSON-XML, é fácil:

types: begin of ts_json,
  data type string,
  tab  type standard table of string with empty key,
end of ts_json.
data ls_data type ts_json.

CALL TRANSFORMATION zst SOURCE XML json RESULT root = ls_data.

Transformação simples ZST (parte interessante somente):

<object tt:ref=".ROOT">
    <str name="data" tt:value-ref="DATA"/>
    <array name="tab">
        <tt:loop ref="TAB">
            <str tt:value-ref="TABLE_LINE"/>
        </tt:loop>
    </array>
</object>

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Não respondo exatamente à sua pergunta, mas quando você conhece a Transformação Simples e JSON-XML, é fácil:

types: begin of ts_json,
  data type string,
  tab  type standard table of string with empty key,
end of ts_json.
data ls_data type ts_json.

CALL TRANSFORMATION zst SOURCE XML json RESULT root = ls_data.

ZST transformação simples (parte interessante apenas):

<object tt:ref=".ROOT">
    <str name="data" tt:value-ref="DATA"/>
    <array name="tab">
        <tt:loop ref="TAB">
            <str tt:value-ref="TABLE_LINE"/>
        </tt:loop>
    </array>
</object>

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019

contacto@primeinstitute.com

(+51) 1641 9379
(+57) 1489 6964

© 2024 Copyright. Todos los derechos reservados.

Desarrollado por Prime Institute

¡Hola! Soy Diana, asesora académica de Prime Institute, indícame en que curso estas interesado, saludos!
Hola ¿Puedo ayudarte?