【问题标题】:D10 to DXE5: what's the equivalent of TJSONBool.Create(False) and JSONObj.ToJSON?D10 到 DXE5:TJSONBool.Create(False) 和 JSONObj.ToJSON 的等价物是什么?
【发布时间】:2017-12-11 02:33:42
【问题描述】:

以下代码在西雅图 D10 上编译良好,但我安装了 D10 的电脑坏了。然后我需要使用 DXE5 在我的项目中进行一个小更新,但由于存在命令 TJSONBool.Create(False)JSONObj.ToJSON 而无法编译。

DXE5 上分别相当于 TJSONBool.Create(False)JSONObj.ToJSON

uses
 Data.DBXJSON, SHFolder;

function GetSpecialFolderPath(folder : integer) : string;
 const
   SHGFP_TYPE_CURRENT = 0;
 var
   path: array [0..MAX_PATH] of char;
 begin
   if SUCCEEDED(SHGetFolderPath(0,folder,0,SHGFP_TYPE_CURRENT,@path[0])) then
     Result := path
   else
     Result := '';
 end;

procedure ChangeChromeSetting(const ATarget, Avalue: string);
var
  specialfolder: integer;
  pathchrome: String;
  JSONObj, ObjIpp: TJSONObject;
  JSONPair: TJSONPair;
  OldValue: string;
begin
  specialFolder := CSIDL_LOCAL_APPDATA;
  pathchrome := GetSpecialFolderPath(specialFolder);
  pathchrome := pathchrome + '\Google\Chrome\User Data\Local State';

 if fileexists(pathchrome) then
  begin
    JSONObj := TJSONObject.ParseJSONValue(TFile.ReadAllText(pathchrome)) as TJSONObject;
    if not Assigned(JSONObj) then Exit; {raise Exception.Create('Cannot read file: ' + pathchrome);}
    try
      OldValue := JSONObj.GetValue<string>(ATarget);
       if OldValue = '' then
               Exit;
      if not SameText(OldValue, Avalue) then
      begin
        JSONPair := JSONObj.Get(ATarget);
        JSONPair.JsonValue.Free;
        JSONPair.JsonValue := TJSONString.Create(Avalue);

        ObjIpp := TJSONObject.Create;
        ObjIpp.AddPair('enabled', TJSONBool.Create(False));
        JSONObj.AddPair('hardware_acceleration_mode', ObjIpp);

        TFile.WriteAllText(pathchrome, JSONObj.ToJSON);
      end;
    finally
      JSONObj.Free;
    end;
  end;
end;

//////////////////////  USAGE /////////////////////////

ChangeChromeSetting('hardware_acceleration_mode_previous', 'false');

【问题讨论】:

    标签: json delphi delphi-xe5


    【解决方案1】:

    TJSONBoolTJSONAncestor.AsJSON 在 XE5 中尚不存在。 XE7 中添加了ToJSON,西雅图 10.0 中添加了TJSONBool

    在旧版本中,请改用TJSONTrue/TJSONFalseTJSONObject.ToString

    ObjIpp.AddPair('enabled', TJSONFalse.Create);
    ... 
    TFile.WriteAllText(pathchrome, JSONObj.ToString);
    

    【讨论】:

      猜你喜欢
      • 2022-07-10
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 2013-10-14
      • 2014-05-08
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多