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

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

问题描述

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



我正在使用以下ABAP函数从json填充现有的ABAP表。 / p>

JSON是正确的,并且Table在表中包含相应的表。

  / ui2 / cl_json =>反序列化(导出json = lv_json 
更改数据= lt_abap)。

运行此命令将返回空白的lt_abap表。



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

  / ui2 / cl_json => deserialize(导出json = lv_json 
更改数据= ls_abap

任何人都可以提出解决方案或将JSON转换为JSON我的表?



JSON:

  {
Id: 1369130,
Venueid: 0005,
Userid: 1320625,
Menuid: null,
Created : 2019-07-29T08:18:35.000 + 0000,
项目:[
{
Id: 4255354,
总计: 3.10,
价格: 2.80,
数量: 1,
Orderid: 1369130,
Menuitemid: 1447268 ,
Externalid:,
名称:早餐前交易,
修饰符:[
{
价格: 0.00 ,
数量: 1,
Id: 0000001,
Orderitemid: 4255354,
外部: 1000716,
名称:培根和香肠玉米卷
},
{
价格: 0.30,
数量: 1,
Id: 00000002,
Orderitemid: 4255354,
Externalid: E1001587 ,
名称:额外的香肠
},
{
价格: 0.00,
数量: 1,
Id: 00000003,
Orderitemid: 4255354,
Externalid: 1000774,
Name: Latte
},
{
价格: 0.00,
数量: 1,
Id: 00000004,
Orderitemid : 4255354,
Externalid: E,
Name:点差
}
]
}
]
}

表结构完全匹配。

解决方案

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



这里是一个示例。

  REPORT ZZZ。 

数据:
g_tab_t000类型标准表t000,带空键。

选择开始。
/ ui2 / cl_json =>反序列化(EXPORTING json ='[{ MANDT: 000},
{ MANDT: 001},{ MANDT: 002 }]'更改数据= g_tab_t000)。
BREAK-POINT。


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

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

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

/ui2/cl_json=>deserialize( EXPORTING json = lv_json
                             CHANGING data = 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

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"
          }
        ]
      }
]
  }

Table Structure matches exactly.

解决方案

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

Here is an example.

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天全站免登陆