将 JSON 反序列化为表 [英] Deserialize JSON to table

查看:51
本文介绍了将 JSON 反序列化为表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据通过 API 接收的数据填充 ABAP 中的表.

I need to populate tables in ABAP from data received through an API.

我正在使用以下 ABAP 函数从 json 填充现有的 ABAP 表.

I'm using the following ABAP function to populate an existing ABAP table from json.

JSON 正确,Table 包含表内对应的表.

The JSON is correct, and the Table contains corresponding tables within tables.

/ui2/cl_json=>deserialize( EXPORTING json = lv_json
                             CHANGING data = lt_abap  ).

运行它会返回一个空白的 lt_abap 表.

Running this returns a blank lt_abap table.

将输出更改为结构时,这可以正常工作.但问题是需要一个表,而不是后续调用的结构

When changing the output to be a structure this works fine. But the problem is a need a TABLE, rather than a STRUCTURE for subsequent calls

/ui2/cl_json=>deserialize( EXPORTING json = lv_json
                             CHANGING data = ls_abap

任何人都可以提出一个解决方案让他将 JSON 放入我的 TABLE 中吗?

Can anyone suggest a solution o get he JSON into my TABLE?

JSON:

{
    "Id": "1369130",
    "Venueid": "0005",
    "Userid": "1320625",
    "Menuid": "null",
    "Created": "2019-07-29T08:18:35.000+0000",
    "items": [
      {
        "Id": "4255354",
        "Total": "3.10",
        "Price": "2.80",
        "Qty": "1",
        "Orderid": "1369130",
        "Menuitemid": "1447268",
        "Externalid": "",
        "Name": "Breakfast Roll Deal",
        "modifiers": [
          {
            "Price": "0.00",
            "Qty": "1",
            "Id": "0000001",
            "Orderitemid": "4255354",
            "Externalid": "1000716",
            "Name": "Bacon and Sausage Corn Topped Roll"
          },
          {
            "Price": "0.30",
            "Qty": "1",
            "Id": "00000002",
            "Orderitemid": "4255354",
            "Externalid": "E1001587",
            "Name": "Extra Sausage"
          },
          {
            "Price": "0.00",
            "Qty": "1",
            "Id": "00000003",
            "Orderitemid": "4255354",
            "Externalid": "1000774",
            "Name": "Latte"
          },
          {
            "Price": "0.00",
            "Qty": "1",
            "Id": "00000004",
            "Orderitemid": "4255354",
            "Externalid": "E",
            "Name": "Spread"
          }
        ]
      }
]
  }

表结构完全匹配.

推荐答案

您发布的内容不是表格.它是一个结构.可以映射到表的 JSON 应该以 [ 开头并以 ] 结尾.

What you posted is not a table. It is a structure. JSON that can be mapped to a table should begin with [ and end with ].

这是一个例子.

REPORT ZZZ.

DATA:
  g_tab_t000 TYPE STANDARD TABLE OF t000 WITH EMPTY KEY.

START-OF-SELECTION.
  /ui2/cl_json=>deserialize( EXPORTING json = '[ { "MANDT": "000" }, 
    { "MANDT": "001" }, { "MANDT": "002" } ]' CHANGING data = g_tab_t000 ).
  BREAK-POINT.

这篇关于将 JSON 反序列化为表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆