如何使用SuperObject从JSON读取DateTime?

时间:2013-10-21 08:44:44

标签: json delphi delphi-xe superobject

我使用SuperObject库来处理JSON。

我有这个JSON(Mozilla FireFox,Chrome书签文件的一部分):

   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13009663942000000",
            "id": "11",
            "meta_info": "{\"sync\":{\"transaction_version\":\"3\"}}",
            "name": "\u041D\u0430\u0447\u0430\u043B\u044C\u043D\u0430\u044F \u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430",
            "type": "url",
            "url": "http://www.mozilla.com/ru/firefox/central/"
         }, {

我尝试使用函数JavaTimeToDelphiDateTime,数据为整数,但它不起作用。

我需要将“date_added”字段读为TDateTime。怎么做,使用SuperObject库?

1 个答案:

答案 0 :(得分:3)

解决方案:

function JavaTimeToDateTime(javatime:Int64):TDateTime;
// java time -> Win32 file time -> UTC time
// adjust to active time zone -> TDateTime
var
  UTCTime, LocalTime: TSystemTime;
begin
  FileTimeToSystemTime(TFileTime(Int64(javatime + 11644473600000) * 10000), UTCTime);
  SystemTimeToTzSpecificLocalTime(nil, UTCTime, LocalTime);
  Result := SystemTimeToDateTime(LocalTime);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //13009663942000000 is the value, read from "date_added" field as Int64.
  ShowMessage(DateTimeToStr(JavaTimeToDateTime((13009663942000000 div 10000))));
end;