This question already has an answer here:
这个问题在这里已有答案:
- How do I convert an ISO 8601 string to a Delphi TDate? 7 answers
- 如何将ISO 8601字符串转换为Delphi TDate? 7个答案
I receive the following JSON from a request:
我从请求中收到以下JSON:
{
"cdPlayer": 3,
"nmPlayer": "Player Name",
"dtCreate": "2016-08-24T22:53:31.687",
"dtChange": null,
"idStatus": true
}
I would like to load convert dtCreate and dtChange to TDateTime. How to properly do this? There is no TJSONDateTime
to cast the GetValue
. Below is my current code where I'm casting it to a plain String.
我想将转换dtCreate和dtChange加载到TDateTime。如何正确地做到这一点?没有TJSONDateTime来强制转换GetValue。下面是我当前的代码,我将它转换为普通的String。
procedure TfrmPrincipal.btnInfoClick(Sender: TObject);
var
jsonRoot: TJSONObject;
tokenRequest: TRESTRequest;
tokenResponse: TRESTResponse;
tokenClient: TRESTClient;
tokenAutenticacao: TOAuth2Authenticator;
begin
tokenClient := TRESTClient.Create(nil);
tokenRequest := TRESTRequest.Create(nil);
tokenResponse := TRESTResponse.Create(nil);
tokenAutenticacao := TOAuth2Authenticator.Create(nil);
try
tokenRequest.Client := tokenClient;
tokenRequest.Response := tokenResponse;
tokenRequest.Method := TRESTRequestMethod.rmPUT;
tokenClient.Authenticator := tokenAutenticacao;
tokenAutenticacao.TokenType := TOAuth2TokenType.ttBEARER;
tokenAutenticacao.AccessToken := 'token_string';
tokenClient.BaseURL := 'http://host/url/method';
tokenRequest.Execute;
jsonRoot:= TJSONObject.ParseJSONValue(tokenResponse.JSONText) as TJSONObject;
Memo1.Lines.Add('cdPlayer => ' + jsonRoot.GetValue('cdPlayer').Value);
Memo1.Lines.Add('nmPlayer=> ' + jsonRoot.GetValue('nmPlayer').Value);
Memo1.Lines.Add('dtCreate=> ' + jsonRoot.GetValue('dtCreate').Value);
Memo1.Lines.Add('dtChange=> ' + jsonRoot.GetValue('dtChange').Value);
Memo1.Lines.Add('idStatus=> ' + (jsonRoot.GetValue('idStatus') as TJSONBool).ToString);
finally
tokenAutenticacao.Free;
tokenResponse.Free;
tokenRequest.Free;
tokenClient.Free;
end;
end;
I'm using Delphi XE 10 Seatle. Thanks
我正在使用Delphi XE 10 Seatle。谢谢
EDIT
编辑
As far as the duplicate goes, I had no ideia that this Data format was ISO 8601. That's why I couldn't find the answer anywhere through Google. Maybe my question will help some others that, like me, didn't know as well.
就副本而言,我并不认为此数据格式是ISO 8601.这就是为什么我无法通过Google找到答案的原因。也许我的问题会帮助其他一些像我一样不了解的人。
1 个解决方案
#1
4
I would extract each part of the date (year, month, day, hours, minutes, milliseconds) from that string, and encode a datetime value. It's easy because each part is always at the same position.
我将从该字符串中提取日期的每一部分(年,月,日,小时,分钟,毫秒),并编码日期时间值。这很简单,因为每个部分始终处于相同的位置。
function JSONDate_To_Datetime(JSONDate: string): TDatetime;
var Year, Month, Day, Hour, Minute, Second, Millisecond: Word;
begin
Year := StrToInt(Copy(JSONDate, 1, 4));
Month := StrToInt(Copy(JSONDate, 6, 2));
Day := StrToInt(Copy(JSONDate, 9, 2));
Hour := StrToInt(Copy(JSONDate, 12, 2));
Minute := StrToInt(Copy(JSONDate, 15, 2));
Second := StrToInt(Copy(JSONDate, 18, 2));
Millisecond := Round(StrToFloat(Copy(JSONDate, 19, 4)));
Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
end;
#1
4
I would extract each part of the date (year, month, day, hours, minutes, milliseconds) from that string, and encode a datetime value. It's easy because each part is always at the same position.
我将从该字符串中提取日期的每一部分(年,月,日,小时,分钟,毫秒),并编码日期时间值。这很简单,因为每个部分始终处于相同的位置。
function JSONDate_To_Datetime(JSONDate: string): TDatetime;
var Year, Month, Day, Hour, Minute, Second, Millisecond: Word;
begin
Year := StrToInt(Copy(JSONDate, 1, 4));
Month := StrToInt(Copy(JSONDate, 6, 2));
Day := StrToInt(Copy(JSONDate, 9, 2));
Hour := StrToInt(Copy(JSONDate, 12, 2));
Minute := StrToInt(Copy(JSONDate, 15, 2));
Second := StrToInt(Copy(JSONDate, 18, 2));
Millisecond := Round(StrToFloat(Copy(JSONDate, 19, 4)));
Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
end;