2017-10-11 4 views
0

いくつかのフィールドにリンクされた2つの一時テーブル用に定義されたデータセットがあります(itemと言うことができます)。たとえば:動的キーを使用してJSONを作成する(進行状況4GL)

define temp-table items no-undo 
    field item as char. 

define temp-table customer no-undo 
    field item  as char serialize-hidden 
    field custname as char 
    field price as dec. 

define dataset dsitemcust for items,customer 
    data-relation dr1 for items,customer relation-fields(item,item) nested. 

これは、このようなJSON出力できます:

{ 
    "items": [ 
    { 
     "item": "abc_item", 
     "customer": [ 
      { 
       "custname": "uvw_cust", 
       "price": 123 
      }, 
      { 
       "custname": "xyz_cust", 
       "price": 234 
      }, 
      .... 
     ] 
    }, 
    { 
     "item": "def_item", 
     "custname": [{},{},...] 
    } 
    ... 
    ] 
} 

をしかし、私はこのような何かを取得したい:キーと、CUSTNAMEや価格など [項目をとしてが再びキーですcustnum)]

{ "abc_item" : [{"uvw_cust" : 123}, {"xyz_cust" : 234}, ...], 
    "def_item" : [{}, .. ], 
    .. 
} 

進捗openedgeに達成可能/このことは可能ですか? (進捗状況:10.2B)

答えて

1

これは可能ですが、手動でJSONオブジェクトを構築する必要があります。

USING Progress.Json.ObjectModel.*. 

DEFINE VARIABLE oJson AS JsonObject NO-UNDO. 
DEFINE VARIABLE oArray AS JsonArray NO-UNDO. 
DEFINE VARIABLE oRec AS JsonObject NO-UNDO. 

DEFINE TEMP-TABLE items NO-UNDO 
    FIELD item AS CHARACTER. 

DEFINE TEMP-TABLE customer NO-UNDO 
    FIELD item  AS CHARACTER 
    FIELD custname AS CHARACTER 
    FIELD price AS DECIMAL. 

/* Create some records here. */ 

oJson = NEW JsonObject(). 

FOR EACH items NO-LOCK: 

    oArray = NEW JsonArray(). 

    FOR EACH customer WHERE customer.item = items.item NO-LOCK BREAK BY customer.item: 
     oRec = NEW JsonObject(). 
     oRec:ADD(customer.custname, customer.price). 
     oArray:ADD(oRec). 
    END. 

    oJson:ADD(items.item, oArray). 
END. 

oJson:WriteFile("test.json", TRUE). 

これはoJsonオブジェクトをファイルに書き込みますが、出力として使用できます。

+0

応答@TheDrooperに感謝します。このコードサンプルはProgress 11.xでもうまくいきますが、残念ながら10.2Bのバージョンでは動作しません。 10.2Bで動作させるための回避策はありますか? – Mahesh

+0

Progress 10.2Bは、あなたの例で持っていたテーブルからJSONだけをサポートしているようです。独自のJSON文字列関数を作成することもできますが、それは多くの作業になる可能性があります。 – TheDrooper

関連する問題