无法创建 ABAP 结构以使用 /ui2/cl_json=>反序列化
Not able to create ABAP structure to use /ui2/cl_json=>deserialize
我的 json 文件如下所示:
{
"1489402": {
"Category": "Program error",
"CorrectionInstructionsObjectList": [{
"ObjectName": "/SCMB/CL_IM_ORG_CHECK IF_EX_HRBAS00_RELAT~MAINTAIN_RELATION",
"ObjectType": "METH",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/MP556100_F01",
"ObjectType": "REPS",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/GET_ORG_STRUCTURE",
"ObjectType": "FUNC",
"ProgramID": "LIMU"
}],
"CurrentStatus": "Released for Customer",
"PrimarySAPComponent": "tm-md-org",
"ReleasedOn": "16.07.2010"
}
}
我想在我的报表中创建一个相应的 ABAP 结构,以便我可以使用这个 json 文件并将其映射到该结构中。我想使用 /ui2/cl_json=>反序列化,但我无法弄清楚接收 ABAP 类型应该是什么。
/ui2/cl_json=>deserialize( EXPORTING json = lv_json_content
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING data = lt_data ).
也就是说lt_data.
的结构应该是什么
不回答你的问题,因为我不精通/ui2/cl_json
,但我提出另一种解决方案。
根据经验,我不建议使用 /ui2/cl_json
,因为据我所知,SAP 并未正式支持它(这只是一名 SAP 员工的倡议),但使用 XSLT 或SAP 简单转换语言(首选)。我选择 XSLT,因为由于 JSON 文件中的动态 属性 名称“1489402”,无法使用 ST。
- 创建 XSLT 转换
- ABAP程序调用转换
请注意,当转换源为JSON时,SAP将其转换为SAP JSON-XML格式(标签如<object>
、<array>
、<str>
) .
如果转换结果是 ABAP 变量(即 RESULT root = variable
,而不是 RESULT XML variable
),XSLT 转换必须 return XML SAP asXML 格式.
XSLT 转换 Z_OBJECTS
:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/object/object">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<ROOT>
<ITEM_NAME>
<xsl:value-of select="@name"/>
</ITEM_NAME>
<CATEGORY>
<xsl:value-of select="str[@name='Category']"/>
</CATEGORY>
<OBJECT_LIST>
<xsl:for-each select="array/object">
<item>
<OBJECT_NAME>
<xsl:value-of select="str[@name='ObjectName']"/>
</OBJECT_NAME>
<OBJECT_TYPE>
<xsl:value-of select="str[@name='ObjectType']"/>
</OBJECT_TYPE>
<PROGRAM_ID>
<xsl:value-of select="str[@name='ProgramID']"/>
</PROGRAM_ID>
</item>
</xsl:for-each>
</OBJECT_LIST>
<CURRENT_STATUS>
<xsl:value-of select="str[@name='CurrentStatus']"/>
</CURRENT_STATUS>
<PRIMARY_SAP_COMPONENT>
<xsl:value-of select="str[@name='PrimarySAPComponent']"/>
</PRIMARY_SAP_COMPONENT>
<RELEASED_ON>
<xsl:value-of select="str[@name='ReleasedOn']"/>
</RELEASED_ON>
</ROOT>
</asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>
ABAP 程序:
TYPES: BEGIN OF ty_object,
object_name TYPE string,
object_type TYPE string,
program_id TYPE string,
END OF ty_object,
ty_object_list TYPE STANDARD TABLE OF ty_object WITH EMPTY KEY,
BEGIN OF ty_item,
item_name TYPE string, " will contain "1489402"
category TYPE string,
object_list TYPE ty_object_list,
current_status TYPE string,
primary_sap_component TYPE string,
released_on TYPE string,
END OF ty_item.
DATA(json) = `{ "1489402": {`
&& ` "Category": "Program error",`
&& ` "CorrectionInstructionsObjectList": [{`
&& ` "ObjectName": "/SCMB/CL_IM_ORG_CHECK IF_EX_HRBAS00_RELAT~MAINTAIN_RELATION",`
&& ` "ObjectType": "METH",`
&& ` "ProgramID": "LIMU"`
&& ` }, {`
&& ` "ObjectName": "/SCMB/MP556100_F01",`
&& ` "ObjectType": "REPS",`
&& ` "ProgramID": "LIMU"`
&& ` }, {`
&& ` "ObjectName": "/SCMB/GET_ORG_STRUCTURE",`
&& ` "ObjectType": "FUNC",`
&& ` "ProgramID": "LIMU"`
&& ` }],`
&& ` "CurrentStatus": "Released for Customer",`
&& ` "PrimarySAPComponent": "tm-md-org",`
&& ` "ReleasedOn": "16.07.2010"}}`.
DATA(item) = VALUE ty_item( ).
CALL TRANSFORMATION z_objects SOURCE XML json RESULT root = item.
注意:为了编写 XSL 转换,您需要知道给定 JSON 的 JSON-XML。你可以使用 ID
转换来了解它。示例:
DATA(json) = `{"a":[1,"s"]}`.
DATA(json_xml) = ``.
CALL TRANSFORMATION id SOURCE XML json RESULT XML json_xml OPTIONS xml_header = 'no'.
ASSERT json_xml+1 = `<object><array name="a"><num>1</num><str>s</str></array></object>`.
你可以试试这个,应该可以的。注意两个额外的反序列化标志,它们控制关联数组的处理以及名称映射以便于重命名。
TYPES:
BEGIN OF ts_cio,
object_name TYPE string,
object_type TYPE string,
program_id TYPE string,
END OF ts_cio,
BEGIN OF ts_error,
category TYPE string,
ci_list TYPE STANDARD TABLE OF ts_cio WITH DEFAULT KEY,
current_status TYPE string,
primary_sap_component TYPE string,
released_on TYPE d,
END OF ts_error,
BEGIN OF ts_dump,
id TYPE i,
error TYPE ts_error,
END OF ts_dump,
tt_dump TYPE SORTED TABLE OF ts_dump WITH UNIQUE KEY id.
DATA: lt_data TYPE tt_dump.
/ui2/cl_json=>deserialize( EXPORTING json = lv_json
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
assoc_arrays = abap_true
assoc_arrays_opt = abap_true
name_mappings = VALUE #(
( abap = `CI_LIST` json = `CorrectionInstructionsObjectList` )
( abap = `PROGRAM_ID` json = `ProgramID` )
( abap = `PRIMARY_SAP_COMPONENT` json = `PrimarySAPComponent` ) )
CHANGING data = lt_data ).
我的 json 文件如下所示:
{
"1489402": {
"Category": "Program error",
"CorrectionInstructionsObjectList": [{
"ObjectName": "/SCMB/CL_IM_ORG_CHECK IF_EX_HRBAS00_RELAT~MAINTAIN_RELATION",
"ObjectType": "METH",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/MP556100_F01",
"ObjectType": "REPS",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/GET_ORG_STRUCTURE",
"ObjectType": "FUNC",
"ProgramID": "LIMU"
}],
"CurrentStatus": "Released for Customer",
"PrimarySAPComponent": "tm-md-org",
"ReleasedOn": "16.07.2010"
}
}
我想在我的报表中创建一个相应的 ABAP 结构,以便我可以使用这个 json 文件并将其映射到该结构中。我想使用 /ui2/cl_json=>反序列化,但我无法弄清楚接收 ABAP 类型应该是什么。
/ui2/cl_json=>deserialize( EXPORTING json = lv_json_content
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING data = lt_data ).
也就是说lt_data.
的结构应该是什么不回答你的问题,因为我不精通/ui2/cl_json
,但我提出另一种解决方案。
根据经验,我不建议使用 /ui2/cl_json
,因为据我所知,SAP 并未正式支持它(这只是一名 SAP 员工的倡议),但使用 XSLT 或SAP 简单转换语言(首选)。我选择 XSLT,因为由于 JSON 文件中的动态 属性 名称“1489402”,无法使用 ST。
- 创建 XSLT 转换
- ABAP程序调用转换
请注意,当转换源为JSON时,SAP将其转换为SAP JSON-XML格式(标签如<object>
、<array>
、<str>
) .
如果转换结果是 ABAP 变量(即 RESULT root = variable
,而不是 RESULT XML variable
),XSLT 转换必须 return XML SAP asXML 格式.
XSLT 转换 Z_OBJECTS
:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/object/object">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<ROOT>
<ITEM_NAME>
<xsl:value-of select="@name"/>
</ITEM_NAME>
<CATEGORY>
<xsl:value-of select="str[@name='Category']"/>
</CATEGORY>
<OBJECT_LIST>
<xsl:for-each select="array/object">
<item>
<OBJECT_NAME>
<xsl:value-of select="str[@name='ObjectName']"/>
</OBJECT_NAME>
<OBJECT_TYPE>
<xsl:value-of select="str[@name='ObjectType']"/>
</OBJECT_TYPE>
<PROGRAM_ID>
<xsl:value-of select="str[@name='ProgramID']"/>
</PROGRAM_ID>
</item>
</xsl:for-each>
</OBJECT_LIST>
<CURRENT_STATUS>
<xsl:value-of select="str[@name='CurrentStatus']"/>
</CURRENT_STATUS>
<PRIMARY_SAP_COMPONENT>
<xsl:value-of select="str[@name='PrimarySAPComponent']"/>
</PRIMARY_SAP_COMPONENT>
<RELEASED_ON>
<xsl:value-of select="str[@name='ReleasedOn']"/>
</RELEASED_ON>
</ROOT>
</asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>
ABAP 程序:
TYPES: BEGIN OF ty_object,
object_name TYPE string,
object_type TYPE string,
program_id TYPE string,
END OF ty_object,
ty_object_list TYPE STANDARD TABLE OF ty_object WITH EMPTY KEY,
BEGIN OF ty_item,
item_name TYPE string, " will contain "1489402"
category TYPE string,
object_list TYPE ty_object_list,
current_status TYPE string,
primary_sap_component TYPE string,
released_on TYPE string,
END OF ty_item.
DATA(json) = `{ "1489402": {`
&& ` "Category": "Program error",`
&& ` "CorrectionInstructionsObjectList": [{`
&& ` "ObjectName": "/SCMB/CL_IM_ORG_CHECK IF_EX_HRBAS00_RELAT~MAINTAIN_RELATION",`
&& ` "ObjectType": "METH",`
&& ` "ProgramID": "LIMU"`
&& ` }, {`
&& ` "ObjectName": "/SCMB/MP556100_F01",`
&& ` "ObjectType": "REPS",`
&& ` "ProgramID": "LIMU"`
&& ` }, {`
&& ` "ObjectName": "/SCMB/GET_ORG_STRUCTURE",`
&& ` "ObjectType": "FUNC",`
&& ` "ProgramID": "LIMU"`
&& ` }],`
&& ` "CurrentStatus": "Released for Customer",`
&& ` "PrimarySAPComponent": "tm-md-org",`
&& ` "ReleasedOn": "16.07.2010"}}`.
DATA(item) = VALUE ty_item( ).
CALL TRANSFORMATION z_objects SOURCE XML json RESULT root = item.
注意:为了编写 XSL 转换,您需要知道给定 JSON 的 JSON-XML。你可以使用 ID
转换来了解它。示例:
DATA(json) = `{"a":[1,"s"]}`.
DATA(json_xml) = ``.
CALL TRANSFORMATION id SOURCE XML json RESULT XML json_xml OPTIONS xml_header = 'no'.
ASSERT json_xml+1 = `<object><array name="a"><num>1</num><str>s</str></array></object>`.
你可以试试这个,应该可以的。注意两个额外的反序列化标志,它们控制关联数组的处理以及名称映射以便于重命名。
TYPES:
BEGIN OF ts_cio,
object_name TYPE string,
object_type TYPE string,
program_id TYPE string,
END OF ts_cio,
BEGIN OF ts_error,
category TYPE string,
ci_list TYPE STANDARD TABLE OF ts_cio WITH DEFAULT KEY,
current_status TYPE string,
primary_sap_component TYPE string,
released_on TYPE d,
END OF ts_error,
BEGIN OF ts_dump,
id TYPE i,
error TYPE ts_error,
END OF ts_dump,
tt_dump TYPE SORTED TABLE OF ts_dump WITH UNIQUE KEY id.
DATA: lt_data TYPE tt_dump.
/ui2/cl_json=>deserialize( EXPORTING json = lv_json
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
assoc_arrays = abap_true
assoc_arrays_opt = abap_true
name_mappings = VALUE #(
( abap = `CI_LIST` json = `CorrectionInstructionsObjectList` )
( abap = `PROGRAM_ID` json = `ProgramID` )
( abap = `PRIMARY_SAP_COMPONENT` json = `PrimarySAPComponent` ) )
CHANGING data = lt_data ).