2016-10-09 9 views
0

私は、既存のJSONファイルで確定したペアの値を変更し、完全に動作するコードを持っています。今私は1つのオブジェクトと1つのペアをこのファイルに追加する必要があります。それではどうやって?1つのオブジェクトと1つのペアを既存の.jsonファイルに追加するにはどうすればよいですか?

ありがとうございます。

uses 
System.Json, ShFolder, System.IOUtils; 

... 

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; 
    caminhochrome: String; 
    JSONObj, Obj: TJSONObject; 
    JSONPair: TJSONPair; 
    OldValue: string; 
begin 
    specialFolder := CSIDL_LOCAL_APPDATA; 
    caminhochrome := GetSpecialFolderPath(specialFolder); 
    caminhochrome := caminhochrome + '\Google\Chrome\User Data\Local State'; 

if fileexists(caminhochrome) then 
    begin 
    Obj := TJSONObject.Create; 
    JSONObj := TJSONObject.ParseJSONValue(TFile.ReadAllText(caminhochrome)) as TJSONObject; 
    if not Assigned(JSONObj) then raise Exception.Create('Cannot read file: ' + caminhochrome); 
    try 
     OldValue := JSONObj.GetValue<string>(ATarget); 
     if not SameText(OldValue, Avalue) then 
     begin 
     JSONPair := JSONObj.Get(ATarget); 
     JSONPair.JsonValue.Free; 
     JSONPair.JsonValue := TJSONString.Create(Avalue); 
     /////////////////////////////////////////////////// 
     Obj.AddPair('enabled', TJSONBool.Create(false)); // Trying add pair 
     JSONObj.AddPair('hardware_acceleration_mode', Obj); // Trying add object 
     ////////////////////////////////////////////////// 
     TFile.WriteAllText(caminhochrome, JSONObj.ToJSON); // Don't add object and pair 
     end; 
    finally 
     JSONObj.Free; 
    end; 
    end; 
end; 


procedure TForm1.Button1Click(Sender: TObject); 
begin 
    ChangeChromeSetting('hardware_acceleration_mode_previous', 'false'); 
end; 

これは私が

"hardware_acceleration_mode":{"enabled":false} 

答えて

0

を待っているという結果であり、あなたのコードは、引数として名前のいくつかを渡すので、やや紛らわしいですが、関数の内部で、ハードコードなどがあります。機能を抽象化するのは良い方法ですが、抽象化する前にコードが正しく機能することを確認する必要があります。抽象ではないコードを表示するつもりです。満足すると、必要に応じて動作し、自由に抽象化することができます。私はメモリリークがないことを保証している

var 
    root: TJSONObject; 
    value: TJSONObject; 
    prev: string; 
begin 
    root := TJSONObject.ParseJSONValue(TFile.ReadAllText(FileName)) as TJSONObject; 
    try 
    prev := root.GetValue<string>('hardware_acceleration_mode_previous'); 
    if not SameText(prev, 'false') then 
    begin 
     // remove existing value, if it exists 
     root.RemovePair('hardware_acceleration_mode').Free; 

     // create a new object, and initialise it 
     value := TJSONObject.Create; 
     value.AddPair('enabled', 'false'); 

     // add the object at the root level 
     root.AddPair('hardware_acceleration_mode', value); 

     // save to file 
     TFile.WriteAllText(FileName, root.ToJSON); 
    end; 
    finally 
    root.Free; 
    end; 
end; 

注:

このコードは、私があなたの意図であると考えているものを行います。 hardware_acceleration_modeという名前の既存の値がある場合、最初に削除されることを確認するために、RemovePairを使用しました。

関連する問題