【发布时间】:2017-09-15 14:39:48
【问题描述】:
我正在使用 Delphi Tokyo 10.2 Update 1 和 RESTRequest、RESTResponse 和 RESTClient 组件与 REST 服务器进行通信。这是我第一次尝试使用 REST/JSON。
我已成功发送登录请求 (POST) 并收到预期的响应 (GUID)。然后我使用 GUID 执行各种其他请求 (GET)。发出的两个请求发回一个空文件和文档 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 中填充字段属性数组中的前两个值,我只需这样做:
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
简单!!!! ;)
使用 Delphi 执行此操作的最佳方法是什么?
我可以从“字段”数组中获取值(本示例中的前两项恰好为空)。只是不确定为这些项目设置值的最佳方式。
这是我开始的:
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;
【问题讨论】:
-
你需要选择一个JSON库,比如Super Object。
-
Delphi 在 RTL 中也有自己的 JSON framework。
-
我已经查看并使用了 RTL 中内置的 JSON 框架。但是,要么我做的不对,要么你不能像使用 Python 那样直接设置 JSON 属性值。我会继续玩 JsonTextReader 和 JsonTextWriter。
-
阅读文档
-
我已阅读文档,@DavidHeffernan。解析和检索值不是问题。设置它们是我遇到问题的地方。并不是说不能做。我在这里看到了例子。我只是很惊讶德尔福没有更好的方法来做到这一点。非常难看。从上面的 Python 源代码中可以看到,它非常简单。 ;) 感谢您的回复。