我从一个URL获取数据,数据以json格式存储在一个变量中。现在我需要解析这个json格式,但我无法解析数据。以下是代码
CLASS zcode_82 DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun .
DATA:lv_response TYPE string,
lv_body type string,
lv_path type string,
lv_json TYPE /ui2/cl_json=>json,
r_json type string.
types: begin of ty_data,
field type string,
id type string,
customer type string,
customer_id type string,
address type string,
date_Created type string,
time_created type string,
END OF TY_DATA.
data: lv_data type STANDARD TABLE OF ty_data with DEFAULT KEY,
lr_data TYPE REF TO data,
ls_data type ty_data.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcode_82 IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA: lv_body_1 TYPE string,
ls_value type RANGE OF ty_data.
TRY.
"create http destination by url; API endpoint for API sandbox
DATA(lo_http_destination) =
cl_http_destination_provider=>create_by_url( 'enter the url ' ).
"create HTTP client by destination
DATA(lo_web_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ) .
"adding headers with API Key for API Sandbox
DATA(lo_web_http_request) = lo_web_http_client->get_http_request( ).
lo_web_http_request->set_header_fields( VALUE #(
( name = 'Authorization' value = 'Bearer key' )
( name = 'Content-Type' value = 'application/json' )
) ).
"set request method and execute request
DATA(lo_web_http_response) = lo_web_http_client->execute( if_web_http_client=>get ).
lv_response = lo_web_http_response->get_text( ).
CATCH cx_http_dest_provider_error cx_web_http_client_error cx_web_message_error.
"error handling
ENDTRY.
* out->write( |response: { lv_response }| ).
*
CLEAR lv_data[].
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_Response
* jsonx =
pretty_name = /ui2/cl_json=>pretty_mode-user
* assoc_arrays =
* assoc_arrays_opt =
* name_mappings =
* conversion_exits =
* hex_as_base64 =
CHANGING
data = lv_data
).
out->write(
EXPORTING
data = lv_data
* name =
* RECEIVING
* output =
).
endmethod.
endclass.
这里,从url下载的数据是输入,输入是
response: {"records":[{"id":"rec5Qk24OQpKDyykq","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010001","address":"Chennai","created_time":"06:00:14","customer":"IDADMIN","date_created":"16.04.2004"}},{"id":"rec7bSe8Zb18z6b5a","createdTime":"2022-08-08T13:07:16.000Z","fields":{"customer_id":"0000010007","address":"Kakinada","created_time":"04:01:18","customer":"Ramya","date_created":"15.04.2000"}},{"id":"recD9Hh4YLgNXOhUE","createdTime":"2022-08-08T11:48:06.000Z","fields":{"customer_id":"0000010002","address":"Bangalore","created_time":"04:03:35","customer":"MAASSBERG","date_created":"20.04.2004"}},{"id":"recK7Tfw4PFAedDiB","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010005","address":"Chennai","created_time":"06:00:49","customer":"IDADMIN","date_created":"21.04.2004"}},{"id":"recKOq0DhEtAma7BV","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010006","address":"Hyderabad","created_time":"18:42:28","customer":"GLAESS","date_created":"21.04.2004"}},{"id":"recS8pg10dFBGj8o7","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010003","address":"Gurugram","created_time":"04:10:02","customer":"MAASSBERG","date_created":"20.04.2004"}},{"id":"recf4QbOmKMrBeLQZ","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010004","address":"Bangalore","created_time":"06:00:12","customer":"IDADMIN","date_created":"21.04.2004"}},{"id":"recs7oHEqfkN87`enter code here`tWm","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010000","address":"Hyderabad","created_time":"04:01:18","customer":"MAASSBERG","date_created":"15.04.2004"}}]}
代码的输出为
Table
FIELD ID CUSTOMER CUSTOMER_ID ADDRESS DATE_CREATED TIME_CREATED
1条答案
按热度按时间jmo0nnb31#
您的结构类型与JSON结构不匹配。您不能简单地跳过外部的
records
数组,而期望反序列化程序知道您想要反序列化其中的内容。与将fields
对象转换为字符串一样,它是对象/结构,而不是字符串。JSON<>ABAP类型如下所示
然后,您可以使用像
DATA ls_response TYPE ty_response
这样的变量作为data
参数。附注:如果可以,考虑使用Simple Transformations with JSON data。在那里,您可以微调命名(混合Snake_Case和CamelCase),需要序列化、反序列化、需要/可选的字段,而且速度更快(尤其是在使用较大的json文件时)。