我想将json字符串解析为abap内部表。
示例json字符串看起来像这样
{" apiVersion":" 1.0","数据":{"位置":"德累斯顿" ,"温度":" 7"," skytext":"小雨","湿度":&#34 ; 96"," wind":" 7.31 km / h"," date":" 02-14-2017",& #34;天":"星期二" }}
我想使用方法
cl_fdt_json=>json_to_data
并将值和键放入这样的表中
types: begin of map,
key type string,
value type string,
end of map.
data json_data type standard table of map.
但不幸的是,它并不像这样。有没有人有这种问题的经验?因为我无法访问所有文档,因为这是我在SAP招聘的样本任务,这是"难题的最后一部分。 ;),我很难找到解决方案。
谢谢!!!
编辑:因此对于vwegerts回答我尝试了以下(这与我原本想做的有点不同,但也没关系)
DATA cl_oops TYPE REF TO cx_dynamic_check.
DATA(text) = result.
TYPES: BEGIN OF ty_structure,
skytext TYPE string,
location type string,
temperature type string,
humidity type string,
wind type string,
date type string,
day type string,
END OF ty_structure.
DATA : wa_structure TYPE ty_structure.
TRY.
CALL TRANSFORMATION id
SOURCE XML text
RESULT data = wa_structure.
message wa_structure-skytext type 'I'.
CATCH cx_transformation_error INTO cl_oops.
WRITE cl_oops->get_longtext( ).
ENDTRY.
但它仍然无效。当我检查wa_structure-skytext的值时,它是不幸的空。我找不到错误。有没有人有想法?
答案 0 :(得分:1)
您可能希望查看w ell-documented capabilities of the ABAP runtime system itself,而不是使用FDT类(可能并非在所有系统上都可用)。 This example program可能是您的最佳选择。您基本上提供了一个简单转换,它将JSON XML结构映射到您的数据结构,实例化一个sXML JSON读取器,然后将其作为源传递给target="_self"
。
答案 1 :(得分:1)
除了@vwegert建议使用SAP记录的json转换外,您还可以查看open source alternatives。 This one看起来很有希望。
答案 2 :(得分:0)
{" apiVersion":" 1.0","数据":{"位置":"德累斯顿" ,"温度":" 7"," skytext":"小雨","湿度":&#34 ; 96"," wind":" 7.31 km / h"," date":" 02-14-2017",& #34;天":"星期二" }}
ABAP中的相应结构将是:
"The nested data table
Types: Begin of ty_data,
location TYPE string,
temperature TYPE string,
skytext TYPE string,
etc.
End of ty_data,
ty_t_data TYPE STANDARD TABLE OF ty_data WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE 0.
"the whole json structure
Types: Begin of ty_json,
apiversion TYPE string,
data TYPE ty_t_data,
End of ty_json.
DATA: ls_data TYPE ty_json.
现在你必须找到一个适当的JSON反序列化器,它可以处理嵌套表。 大多数反序列化器都需要输入表格,因此您必须添加' [' ...']'在JSON字符串的末尾,并定义lt_data TYPE STANDARD TABLE OF ty_json。
答案 3 :(得分:0)
您可以通过SAP JSON-XML阅读器来做到这一点:
CLASS lcl_json DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF map,
key TYPE string,
value TYPE string,
END OF map,
tt_map TYPE STANDARD TABLE OF map WITH DEFAULT KEY.
CLASS-METHODS: parse IMPORTING iv_json TYPE string
RETURNING VALUE(rv_map) TYPE tt_map.
ENDCLASS.
CLASS lcl_json IMPLEMENTATION.
METHOD parse.
DATA(o_reader) = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( iv_json ) ).
TRY.
DATA(o_node) = o_reader->read_next_node( ).
WHILE o_node IS BOUND.
CASE o_node->type.
WHEN if_sxml_node=>co_nt_element_open.
DATA(op) = CAST if_sxml_open_element( o_node ).
LOOP AT op->get_attributes( ) ASSIGNING FIELD-SYMBOL(<a>).
APPEND VALUE #( key = <a>->get_value( ) ) TO rv_map ASSIGNING FIELD-SYMBOL(<json>).
ENDLOOP.
WHEN if_sxml_node=>co_nt_value.
DATA(val) = CAST if_sxml_value_node( o_node ).
<json>-value = val->get_value( ).
WHEN OTHERS.
ENDCASE.
o_node = o_reader->read_next_node( ).
ENDWHILE.
CATCH cx_root INTO DATA(e_txt).
RAISE EXCEPTION TYPE cx_sxml_parse_error EXPORTING error_text = e_txt->get_text( ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(json_string) = ` {"apiVersion":"1.0", ` &&
` "data":{ "location":"Dresden", "temperature":"7",` &&
` "skytext":"Light rain", "humidity":"96", "wind":"7.31 km/h", "date":"02-14-2017", "day":"Tuesday" } } `.
TRY.
DATA(it_map) = lcl_json=>parse( json_string ).
CATCH cx_root INTO DATA(e_txt).
" do handling
ENDTRY.