2016-04-20 3 views
1

Datasnapで返されたデータセットの結果をローテーションしようとしています。DatasetでデータセットJSONの結果を回転する(Delphi 10)

サンプル:これは私が

{"result":[{"table":[["REG_KEY",1,0,0,2,3,0,0,false,false,0,false,false],["REG_NAME",1,1,0,128,129,0,0,false,false,0,false,false]],"REG_KEY":["01","02"],"REG_NAME":["BALEARES","CANARIAS"]}]} 

得るものであり、私はちょうどそれを変換したい:

[{"REG_KEY":"01","REG_NAME":"BALEARES"},{"REG_KEY":"02","REG_NAME":"CANARIAS"}] 

私はDSHTTPWebDispatcher.FormatResult以内にそれを変換するつもりが、私の最初の時間ですDataSnap/REST/JSONを使用しているため、JSON値を管理するためのクラスの理解に問題があります。

それはこのようなものでなければなりません:

procedure TWebModule1.DSHTTPWebDispatcher1FormatResult(Sender: TObject; var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean); 
var 
    Aux: TJSONObject; 
    NewResult: TJSONObject; 
    Row: TJSONObject; 
    NumField, NumRow, MaxFields, MaxRows: integer; 
begin 
    Handled := True; 
    NewResult := TJSONArray.Create; 
    Aux := TJSONArray(ResultVal).Get(0); // I get the result as an Object instead of an Array 
    MaxFields := Aux.Pairs.Count - 1; // I ignore the Pair 0 because it only contains the Dataset structure 
    MaxRows := TJSONArray(1).Count; 
    for NumRow := 0 to MaxRows - 1 do begin 
     Row := TJSONObject.Create; 
     for NumField := 1 to MaxFields do begin 
      Row.AddPair(Aux.Pairs[NumField].JsonString, 
         TJSONArray(Aux.Pairs[NumField].JsonValue).Get(NumRow)); 
     end; 
     Aux.Add(Row); 
    end; 
    Aux.Free; 
    ResultVal.Free; 
    ResultVal := NewResult; 
end; 

しかしTJSONObjectが含まれているどのように多くのペアを知っているPairs.Countメソッドを持っていない、とTJSONArrayを直接追加する必要がありますし、メソッドを追加しません。新しいオブジェクト。このコードには他の多くのエラーがあることは間違いありません。

私はそれを修正する方法を教えてもらえますか?

答えて

2

私は、このコードは、私が探した変換(それは、行と列を反復処理し、二重ループを行い、それが新しいJSONで結果を回転させる)し、それを:-)

を得た:

procedure TWebModule1.DSHTTPWebDispatcher1FormatResult(Sender: TObject; var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean); 
var Aux: TJSONObject; 
    NewResult: TJSONValue; 
    Row: TJSONObject; 
    NumField, NumRow, MaxFields, MaxRows: integer; 
begin 
    Handled := True; 

    if ResultVal.ToJSON = '[{"table":[]}]' then begin 
    TJSONArray(ResultVal).Remove(0); 
    end 
    else if (LowerCase(Copy(ResultVal.ToJSON, 1, 10)) = '[{"table":') then begin // For Datasets I rotate its content so it comes as rows and not as columns 
    NewResult := TJSONArray.Create; 
    Aux := TJSONObject(TJSONArray(ResultVal).Get(0)); 

    MaxFields := Aux.Count - 1; // I ignore the Pair 0 because it only contains the Dataset structure 
    MaxRows := TJSONArray(TJSONPair(Aux.Get(1)).JSONValue).Count; 

    for NumRows := 0 to MaxRows - 1 do begin 
     Row := TJSONObject.Create; 
     for NumFields := 1 to MaxFields do begin 
     Row.AddPair(Aux.Pairs[NumField].JsonString.Value, 
        TJSONArray(Aux.Pairs[NumField].JsonValue).Get(NumRow).Clone as TJSONValue); 
     end; 
     TJSONArray(NewResult).AddElement(Row); 
    end; 

    ResultVal.Free;    // I free the old Result because a new one is returned (Aux is already supposed to be freed when freeing ResultVal) 
    ResultVal := NewResult;  
    end; 
end; 
をWebモジュールでこれを入れ
2

procedure TWebModule1.DSHTTPWebDispatcher1FormatResult(Sender: TObject; 
var ResultVal: TJSONValue; const Command: TDBXCommand; 
       var Handled: Boolean); 
var 
    Aux: TJSONValue; 
begin 
    Aux := ResultVal; 
    ResultVal := TJSONArray(Aux).Get(0); 
    TJSONArray(Aux).Remove(0); 
    Aux.Free; 
end; 

あなたの結果は、この

{"result":[{...},{...}]} 
になります
+0

ありがとうございますが、これは私が探していたものではありません。すべてのレコードをメイン配列の要素として返す結果を返す必要がありますが、Datasnapはすべてのフィールドを値の異なる配列として返します: [{"REG_KEY": "01"、 "REG_NAME": "BALEARES"}、 [REG_KEY]:["01"、 "02"]、 "REG_NAME":["BALEARES"、 "REG_KEY": "02"、 "REG_NAME": "CANARIAS"}] の代わりに の代わりに、 CANARIAS "]}] –

関連する問題