小编Rob*_*Rob的帖子

Delphi XE7:如何使用 System.JSON(与 SuperObject)更改 JSON 值

我需要加载一个 JSON 文件,更改一个值,然后将其写回磁盘。

使用 SuperObject 很容易,但我如何使用 System.JSON 单元做同样的事情?

const
  PathToX = 'AllCategories.Category[0].subCategory[0].products[0].views.view[0].x';

var
  JsonFilename: string;

  JO: ISuperObject; // from the SuperObject unit
  JV: TJsonValue;   // from the System.Json unit

begin
  JsonFilename := ExtractFilePath(Application.ExeName)+'product.json');

  // Using the SuperObject unit:
  JO := SO(TFile.ReadAllText(JsonFilename));

  WriteLn('The old value of "x" is ', JO[PathToX].AsString);
  WriteLn('Changing value of x to "123"');
  JO.S[PathToX] := '123';   // Set the value of "x"
  WriteLn('The new value of "x" is ', JO[PathToX].AsString);

  // Now trying to do the same thing …
Run Code Online (Sandbox Code Playgroud)

delphi json system.json superobject

3
推荐指数
1
解决办法
7184
查看次数

标签 统计

delphi ×1

json ×1

superobject ×1

system.json ×1