将 JSON 转换为 ABAP 时遇到问题。这是一个高度简化的示例,但试图捕获要点。每个 JSON 元素(除了少数异常(exception))都可以存在实值、空值或根本不存在。
在此示例中,有 3 个订单商品。订单商品可能有也可能没有 order_reason。对于第 01 行,它是 ABC,对于第 02 行,它是空,对于第 03 行,它根本不存在。
如果您查看 XML,您会发现创建的元素根据内容而变化,我不知道如何在 ST 中处理它。我猜这是某种条件、组或开关,但我似乎找不到正确的语法来检查元素是否有时为“空”或有时为“字符串”。因此,它不是对元素内容的检查,而是对实际元素本身的检查。
super 简单的 JSON
{
"order_id": "564320",
"items": [
{
"line": "01",
"order_reason": "ABC"
},
{
"line": "02",
"order_reason": null
},
{
"line": "03"
}
]
}
然后将其隐式转换为 XML,以便在 ST 中进行处理。第一项的订单原因元素为
<object>
<str name="order_id">564320</str>
<array name="items">
<object>
<str name="line">01</str>
<str name="order_reason">ABC</str>
</object>
<object>
<str name="line">01</str>
<null name="order_reason" />
</object>
<object>
<str name="line">03</str>
</object>
</array>
</object>
这是我的示例 ST。
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" >
<tt:root name="ROOT" />
<tt:template>
<object>
<str name="order_id">
<tt:value ref="ROOT.order_id"/>
</str>
<array>
<tt:loop ref="ROOT.items">
<object>
<str name="line">
<tt:value ref="$ref.posnr"/>
</str>
<str name="order_reason">
<tt:value ref="$ref.reason"/>
</str>
</object>
</tt:loop>
</array>
</object>
</tt:template>
</tt:transform>
这是一个可执行的ABAP来测试上述内容。有两个版本的 JSON 可供测试。第一个有效,因为每个项目都有一个原因代码。检查这个只是为了确保一切都在快乐的情况下工作。通过切换到代码中的第二个示例,您可以看到需要有一些东西来处理
*&---------------------------------------------------------------------*
*& Report Z_JSON_ABAP
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_json_abap2.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
TYPES: BEGIN OF ty_s_line,
posnr TYPE posnr,
reason TYPE char3,
END OF ty_s_line.
TYPES: BEGIN OF ty_s_header,
order_id TYPE vbeln,
items TYPE TABLE OF ty_s_line WITH DEFAULT KEY,
END OF ty_s_header.
DATA: lt_order TYPE ty_s_header.
* Example 1 - Works - Easy!
DATA(lv_json) = cl_abap_codepage=>convert_to(
`{` &&
` "order_id": "51324", ` &&
` "items": [ ` &&
` { ` &&
` "line": "01", ` &&
` "order_reason": "ABC" ` &&
` }, ` &&
` { ` &&
` "line": "02", ` &&
` "order_reason": "DEF" ` &&
` }, ` &&
` { ` &&
` "line": "03", ` &&
` "order_reason": "123" ` &&
` } ` &&
` ] ` &&
` } ` ).
* Example 2 - Does not work! - Swap sections to test
* DATA(lv_json) = cl_abap_codepage=>convert_to(
* `{` &&
* ` "order_id": "51324", ` &&
* ` "items": [ ` &&
* ` { ` &&
* ` "line": "01", ` &&
* ` "order_reason": "ABC" ` &&
* ` }, ` &&
* ` { ` &&
* ` "line": "02", ` &&
* ` "order_reason": null ` &&
* ` }, ` &&
* ` { ` &&
* ` "line": "03" ` &&
* ` } ` &&
* ` ] ` &&
* ` } ` ).
CALL TRANSFORMATION zst_order_test SOURCE XML lv_json RESULT root = lt_order.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
最佳答案
事实证明这太简单了。我可能盯着它看了太久,并用组和开关等想象了一些过于复杂的解决方案。也许这甚至是可能的,但这是可行的。刚刚添加了两个条件元素。
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
<tt:root name="ROOT"/>
<tt:template>
<object>
<str name="order_id">
<tt:value ref="ROOT.order_id"/>
</str>
<array>
<tt:loop ref="ROOT.items">
<object>
<str name="line">
<tt:value ref="$ref.posnr"/>
</str>
<tt:cond>
<str name="order_reason">
<tt:value ref="$ref.reason"/>
</str>
</tt:cond>
<tt:cond>
<null name="order_reason">
<tt:value ref="$ref.reason"/>
</null>
</tt:cond>
</object>
</tt:loop>
</array>
</object>
</tt:template>
</tt:transform>
关于json - 通过 ST 转换处理 JSON 反序列化中的 Null 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66407706/