openedge write-json 如何添加对象

openedge write-json how to add object

这是我想要的输出

{ "event_type": "LOAD_AVAILABLE",
  "event_data": {
  "load": [
    {
      "Ord": "ORDER12344",
      "ShipReqDt": "2021-10-29",
      "load_items": [
        {
          "ItemDesc": "Apple 12oz English Domestic: 10004-000",
          "Qty": 320
        },
        {
          "ItemDesc": "Apple CTN 12",
          "Qty": 980
        }
      ]
    }
  ]
}}

这是我的代码

def temp-table ttOrd serialize-name "load"
  field Ord       like Ord.Ord
  field ShipReqDt like Ord.ShipReqDt.    
def temp-table ttItem serialize-name "load_items"
  field Ord       like OrdDet.Ord serialize-hidden
  field ItemDesc  as char
  field Qty       like OrdDet.ActBox.
  
def dataset dsOrd serialize-name "event_data" for 
  ttOrd, ttItem
  data-relation Rel1 for ttOrd, ttItem nested relation-fields(Ord, Ord).

procedure WriteJSON:
def var dFilename    as char                 no-undo.
  
  dFilename    = dSessTmpDir + "/Test1.xml".
  
  find Ord no-lock where
       Ord.Ord = "ORDER12344"
       no-error.
  create ttOrd.
  assign
    ttOrd.Ord = Ord.Ord
    ttOrd.ShipReqDt = Ord.ShipReqDt.
  
  for each OrdDet where OrdDet.Ord = Ord.Ord on error undo, return error on stop undo, return error:
      
    Create ttItem.
    assign
      ttItem.Ord = OrdDet.Ord
      ttItem.ItemDesc = OrdDet.Descr  
      ttItem.Qty      = OrdDet.Qty.
    
  end. 
  
  dataset dsOrd:write-json("FILE", dFilename, true).
  
end procedure. /* WriteJSON */

我的期望输出中唯一缺少的是第一行中的一个对象

"event_type": "LOAD_AVAILABLE"

关于如何添加它们有什么建议吗?

谢谢

您可以先将数据集的 JSON 加载到 JSON 对象中:

在程序的顶部(第一条语句):

USING Progress.Json.ObjectModel.* FROM PROPATH.

DEFINE VARIABLE oJsonObject AS JsonObject NO-UNDO .
DEFINE VARIABLE oJsonObject2 AS JsonObject NO-UNDO .

而不是你的 dsOrd:WRITE-JSON

oJsonObject = NEW JsonObject () .

dataset dsOrd:write-json("JsonObject", oJsonObject, true).

oJsonObject2 = NEW JsonObject () .
oJsonObject2:Add ("event_type", "LOAD_AVAILABLE") .
oJsonObject2:Add ("event_data", oJsonObject:GetJsonObject ("event_data") .
oJsonObject2:WriteFile(dFileName, TRUE) .