2017-09-15 5 views
0

私はDelphi Tokyo 10.2 Update 1とRESTRequest、RESTResponse、RESTClientコンポーネントを使用してRESTサーバーと通信しています。これはREST/JSONでの最初の試みです。Delphi/REST/JSON

私は正常にログイン要求(POST)を送信し、期待される応答(GUID)を受け取りました。その後、GUIDを使用してさまざまな他のリクエスト(GET)を行います。リクエストのうち2つが空のファイルを返送し、JSONテンプレートをドキュメント化し、次にそれを移入する必要があります。これは私が立ち往生している場所です。 JSONオブジェクトのプロパティ値を更新する最善の方法が不明です。ここで

は、私は戻って取得しています空のJSONファイルのテンプレートです:

{ 
    "boxId": 0, 
    "changedBy": 0, 
    "customSort": "", 
    "dateChanged": "1990-01-01T00:00:00", 
    "dateStarted": "1990-01-01T00:00:00", 
    "destruction": "1990-01-01T00:00:00", 
    "documentCount": 0, 
    "documents": { 
    "TotalCount": 0, 
    "Collection": [ 

    ] 
    }, 
    "extraData": { 
    "TotalCount": 0, 
    "Collection": [ 

    ] 
    }, 
    "fieldDefs": { 
    "TotalCount": 0, 
    "Collection": null 
    }, 
    "field": [ 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "", 
    "" 
    ], 
    "fileId": 0, 
    "filePtr": 0, 
    "id": 0, 
    "isIndexed": false, 
    "keyValue": "", 
    "keyVisualValue": "", 
    "labelPrinted": "1990-01-01T00:00:00", 
    "lineItems": { 
    "TotalCount": 0, 
    "Collection": [ 

    ] 
    }, 
    "notes": "", 
    "objectType": 5, 
    "projectId": 0, 
    "routeInfo": null, 
    "routingDoc": null, 
    "remoteId": 0, 
    "saveNotesOnly": false, 
    "saveStyle": -999, 
    "status": 1, 
    "syncFlag": 0, 
    "totalDocumentCount": 0, 
    "viewerContext": 0 
} 

Pythonでは、私は単純にどうなるフィールドプロパティ配列の最初の2つの値を移入します

inc_filetemplate = json.loads(requests.get(NEWFILE_string).text) 
inc_doctemplate = json.loads(requests.get(NEWDOC_string).text) 
filetemplate = inc_filetemplate 
doctemplate = inc_doctemplate 
filetemplate['field'][1] = dcn 
filetemplate['field'][2] = batchname 

EASYを!!!! ;)

これを行うには、どのような方法が最適ですか?

「フィールド」配列から値を取得できます(この例では最初の2つは空になります)。これらの項目の値を設定する最も良い方法は不明です。

これは私が始めたものである:

procedure PopulateFileTemplate(const AFileTemplate: String); 
var 
    JO: TJSONObject; 
    JOPair: TJSONPair; 
    JOArray: TJSONArray; 
    FieldDCN: String; 
    FieldBatchName: String; 

begin 
    JO := TJSONObject.ParseJSONValue(AFileTemplate) as TJSONObject; 

    try 
    if JO = nil then 
    begin 
     MessageDlg('Unable to parse JSON file template.', mtError, [mbOK], 0); 

     Exit; 
    end; 

    JOArray := JO.Get('field').JsonValue as TJSONArray; 
    FieldDCN := JOArray.Items[0].Value; 
    FieldBatchName := JOArray.Items[1].Value; 

    Memo1.Lines.Add('The old value of DCN is: ' + FieldDCN); 
    Memo1.Lines.Add('The old value of BatchName is: ' + FieldBatchName); 

    // Best way to set Values here??????? 

    Memo1.Lines.Add('The new value of DCN is: ' + FieldDCN); 
    Memo1.Lines.Add('The new value of BatchName is: ' + FieldBatchName); 
    finally 
    JO.Free; 
    end; 
end; 
+0

スーパーオブジェクトなどのJSONライブラリを選択する必要があります。 –

+0

Delphiには独自の[JSONフレームワーク](http://docwiki.embarcadero.com/RADStudio/ja/JSON)もRTLに組み込まれています。 –

+0

私はRTLに組み込まれたJSONフレームワークを見て遊んでいます。しかし、私は何か正しいことをしていないか、JSONプロパティ値をPythonでできる限り簡単に設定することはできません。私はJsonTextReaderとJsonTextWriterで遊んでいきます。 – klbass68

答えて

0

TJSONArray.Items[]プロパティは、あなたの例ではTJSONStringオブジェクトがfieldsので、文字列の配列である、TJSONValueを返します。

TJSONArrayは実際にはの既存のオブジェクトを変更できるように設計されていません。新しいオブジェクトを配列の最後に追加して、任意のオブジェクトを配列から削除できますが、任意のインデックスに新しいオブジェクトを挿入したり、既存のオブジェクトを新しいオブジェクトに置き換えることはできません。

この種の機能に最も近いのは、TList<TJSONValue>を目的のオブジェクトで構成してからTJSONArray.SetElements()に渡すことです。理想的ではない。

TJSONStringには、string値を編集するためのメソッドやプロパティはありません(TJSONString.AddChar()を除く)。

あなたが保護さTJSONString.FStrBufferメンバーへのアクセスを獲得し、必要に応じてその内容を編集するアクセサ/ヘルパークラスを使用して試みることができる:

type 
    TJSONStringAccess = class(TJSONString) 
    end; 

procedure SetJSONStringValue(JSONValue: TJSONString; const S: string); 
begin 
    with TJSONStringAccess(JSONValue) do 
    begin 
    FStrBuffer.Clear; 
    FStrBuffer.Append(S); 
    end; 
end; 

procedure PopulateFileTemplate(const AFileTemplate: String); 
var 
    JO: TJSONObject; 
    JOPair: TJSONPair; 
    JOArray: TJSONArray; 
    FieldDCN: TJSONString; 
    FieldBatchName: TJSONString; 
begin 
    JO := TJSONObject.ParseJSONValue(AFileTemplate) as TJSONObject; 
    if JO = nil then 
    begin 
    MessageDlg('Unable to parse JSON file template.', mtError, [mbOK], 0); 
    Exit; 
    end; 
    try 
    JOArray := JO.Get('field').JsonValue as TJSONArray; 
    FieldDCN := JOArray.Items[0] as TJSONString; 
    FieldBatchName := JOArray.Items[1] as TJSONString; 

    Memo1.Lines.Add('The old value of DCN is: ' + FieldDCN.Value); 
    Memo1.Lines.Add('The old value of BatchName is: ' + FieldBatchName.Value); 

    SetJSONStringValue(FieldDCN, '...'); 
    SetJSONStringValue(FieldBatchName, '...'); 

    Memo1.Lines.Add('The new value of DCN is: ' + FieldDCN.Value); 
    Memo1.Lines.Add('The new value of BatchName is: ' + FieldBatchName.Value); 
    finally 
    JO.Free; 
    end; 
end; 

type 
    TJSONStringHelper = helper class for TJSONString 
    procedure SetValue(const S: string); 
    end; 

procedure TJSONStringHelper.SetValue(const S: string); 
begin 
    Self.FStrBuffer.Clear; 
    Self.FStrBuffer.Append(S); 
end; 

procedure PopulateFileTemplate(const AFileTemplate: String); 
var 
    JO: TJSONObject; 
    JOPair: TJSONPair; 
    JOArray: TJSONArray; 
    FieldDCN: TJSONString; 
    FieldBatchName: TJSONString; 
begin 
    JO := TJSONObject.ParseJSONValue(AFileTemplate) as TJSONObject; 
    if JO = nil then 
    begin 
    MessageDlg('Unable to parse JSON file template.', mtError, [mbOK], 0); 
    Exit; 
    end; 
    try 
    JOArray := JO.Get('field').JsonValue as TJSONArray; 
    FieldDCN := JOArray.Items[0] as TJSONString; 
    FieldBatchName := JOArray.Items[1] as TJSONString; 

    Memo1.Lines.Add('The old value of DCN is: ' + FieldDCN.Value); 
    Memo1.Lines.Add('The old value of BatchName is: ' + FieldBatchName.Value); 

    FieldDCN.SetValue('...'); 
    FieldBatchName.SetValue('...'); 

    Memo1.Lines.Add('The new value of DCN is: ' + FieldDCN.Value); 
    Memo1.Lines.Add('The new value of BatchName is: ' + FieldBatchName.Value); 
    finally 
    JO.Free; 
    end; 
end; 

そうでない場合は、第三に切り替えることを検討ネイティブに編集値をサポートするJSONライブラリがあります。例えば、。

+0

ありがとうございました。 SuperObjectのほうが良い方法かもしれないようです。私の研究では、私はそれを使っている人々を見ました。今すぐダウンロードしてください。再度、感謝します! – klbass68