[英]What kind of data structure is needed to parse JSON to itab?
I want to parse a json string into an abap internal table, for example, this one我想把一个json字符串解析成一个abap内表,比如这个
{
"apiVersion": "1.0",
"data": {
"location": "Dresden",
"temperature": "7",
"skytext": "Light rain",
"humidity": "96",
"wind": "7.31 km/h",
"date": "02-14-2017",
"day": "Tuesday"
}
}
I want to use the method cl_fdt_json=>json_to_data and put the values and keys into a table like this我想使用方法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.
But, unfortunately, it does not work like that.但是,不幸的是,它不是那样工作的。 Does anyone have experience with this kind of problem?有没有人遇到过这种问题? I don't have access to all the documentation because this is my sample task for a hirement to SAP and this is the last part of the "puzzle";) It is hard for me to find the solution.我无权访问所有文档,因为这是我聘用 SAP 的示例任务,这是“难题”的最后一部分;)我很难找到解决方案。
Thank you!!!谢谢!!!
EDIT: accordingly to vwegerts answer I tried the following.编辑:根据 vwegerts 的回答,我尝试了以下操作。 This is a little bit different to what i originally wanted to do, but it would also be ok)这与我最初想做的有点不同,但也可以)
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.
but it still doesnt work.但它仍然不起作用。 when i check the value of wa_structure-skytext it is unfortunatly empty.当我检查 wa_structure-skytext 的值时,不幸的是它是空的。 i cannot find the mistake.我找不到错误。 does anyone have an idea?有人有想法吗?
Rather than use the FDT class (which might not be available on all systems), you might want to take a look at the w ell-documented capabilities of the ABAP runtime system itself .与其使用 FDT 类(它可能并非在所有系统上都可用),您可能还想看看ABAP 运行时系统本身的有据可查的功能。 This example program might be the way to go for you. 这个示例程序可能适合您。 You would essentially provide a Simple Transformation that would map the JSON XML structure to your data structure, instantiate a sXML JSON reader and then pass that as source to CALL TRANSFORMATION
.您基本上会提供一个简单转换,将JSON XML结构映射到您的数据结构,实例化一个 sXML JSON 读取器,然后将其作为源传递给CALL TRANSFORMATION
。
Besides @vwegert recommendation to use the SAP documented json transformations, you could check the open source alternatives .除了 @vwegert 建议使用 SAP 记录的 json 转换之外,您还可以查看开源替代方案。 This one looks promising.这个看起来很有希望。
{"apiVersion":"1.0", "data":{ "location":"Dresden", "temperature":"7", "skytext":"Light rain", "humidity":"96", "wind":"7.31 km/h", "date":"02-14-2017", "day":"Tuesday" } } {"apiVersion":"1.0", "data":{ "location":"德累斯顿", "temperature":"7", "skytext":"小雨", "humidity":"96", "wind" :"7.31 km/h", "date":"02-14-2017", "day":"星期二" } }
The corresponding structure in ABAP would be: 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.
Now you have to find a proper JSON deserializer, which handles nested tables.现在你必须找到一个合适的 JSON 反序列化器,它可以处理嵌套表。 Most deserializer expect a table input, so you have to add '['... ']' at the end of your JSON string and define lt_data TYPE STANDARD TABLE OF ty_json.大多数反序列化器都需要一个表输入,所以你必须在你的 JSON 字符串的末尾添加 '['... ']' 并定义 lt_data TYPE STANDARD TABLE OF ty_json。
You can do it like that via SAP JSON-XML reader:您可以通过 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.