如何为现有的.json文件添加一个对象和一对对象? [英] How add one object and one pair for a existent .json file?

查看:141
本文介绍了如何为现有的.json文件添加一个对象和一对对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,可以更改现有JSON文件中一个确定的对的值,并且可以完美地工作.现在,我需要使用相同的代码在很大程度上添加一个对象和一对对象到该文件.那怎么办呢?

I have a code that changes a value of a determinated pair in a existent JSON file and works perfectly. Now i need add one object and one pair to this file, using great part this same code. So how do this?

谢谢.

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}

推荐答案

您的代码有些混乱,因为您传入了一些名称作为参数,但是随后在函数中硬编码了其他名称.抽象功能是一种很好的做法,但是在进行抽象之前,您确实需要确保代码正确运行.我将展示不会尝试抽象的代码.一旦您满意,它就会按需运行,然后随意进行抽象.

Your code is somewhat confusing since you pass in some of the names as arguments, but then hard code others inside the function. Abstracting functionality is good practise but before you can abstract you really need to ensure the code works correctly. I'm going to show code that does not attempt to be abstract. Once you are satisfied it behaves as you need, then feel free to abstract away.

此代码符合我的意图:

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;

请注意,我已确保没有内存泄漏.我已经使用RemovePair来确保是否有一个名为hardware_acceleration_mode的现有值被首先删除.

Note that I have ensured that there are no memory leaks. I've used RemovePair to make sure that if there is an existing value named hardware_acceleration_mode it is first removed.

这篇关于如何为现有的.json文件添加一个对象和一对对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆