无法创建 ABAP 结构以使用 /ui2/cl_json=>反序列化



我的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 Simple Transformation语言(首选)。我选择XSLT是因为由于动态属性名为"1489402",所以无法使用ST。

  1. 创建XSLT转换
  2. ABAP程序调用转换

注意,当转换源是JSON时,SAP将其转换为SAP JSON- xml格式(标签如<object>,<array>,<str>)。

如果转换结果是ABAP变量(即RESULT root = variable,而不是RESULT XML variable), XSLT转换必须返回SAP asXML格式的XML。

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 ).

相关内容

  • 没有找到相关文章

最新更新