如何使用 SuperObject lib 在 Delphi 中创建 JSON 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16790006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create JSON-file in Delphi using SuperObject lib?
提问by Romowski
I'm using Delphi 2010 and superobjectlibrary.
我正在使用 Delphi 2010 和超级对象库。
I have understand how to parse json-file, but I have no ideas how to create json?
我了解如何解析 json 文件,但我不知道如何创建 json?
The algorithm is:
算法是:
- Parsing JSON and load in TStringGrid
- Adding data
- Save all TStringGrid data to json.
- 解析 JSON 并在 TStringGrid 中加载
- 添加数据
- 将所有 TStringGrid 数据保存到 json。
Need some example.
需要一些例子。
Thanks.
谢谢。
回答by Marko Paunovic
Code sample to feed following structure to JSON object, then save to file:
将以下结构提供给 JSON 对象的代码示例,然后保存到文件:
(*
{
"name": "Henri Gourvest", /* this is a comment */
"vip": true,
"telephones": ["000000000", "111111111111"],
"age": 33,
"size": 1.83,
"addresses": [
{
"address": "blabla",
"city": "Metz",
"pc": 57000
},
{
"address": "blabla",
"city": "Nantes",
"pc": 44000
}
]
}
*)
procedure SaveJson;
var
json, json_sub: ISuperObject;
begin
json := SO;
json.S['name'] := 'Henri Gourvest';
json.B['vip'] := TRUE;
json.O['telephones'] := SA([]);
json.A['telephones'].S[0] := '000000000';
json.A['telephones'].S[1] := '111111111111';
json.I['age'] := 33;
json.D['size'] := 1.83;
json.O['addresses'] := SA([]);
json_sub := SO;
json_sub.S['address'] := 'blabla';
json_sub.S['city'] := 'Metz';
json_sub.I['pc'] := 57000;
json.A['addresses'].Add(json_sub);
json_sub.S['address'] := 'blabla';
json_sub.S['city'] := 'Nantes';
json_sub.I['pc'] := 44000;
json.A['addresses'].Add(json_sub);
json.SaveTo('C:\json_out.txt');
json := nil;
json_sub := nil;
end;
回答by Arioch 'The
Reading help file: https://github.com/hgourvest/superobject/blob/master/README.md
阅读帮助文件:https: //github.com/hgourvest/superobject/blob/master/README.md
And then reading sources for TSuperArray ('Use the source, Luke')
然后读取 TSuperArray 的源代码('使用源代码,Luke')
Results in the following snippet:
结果在以下代码段中:
var
obj: ISuperObject;
a: TSuperArray; // shortcut
begin
obj := TSuperObject.Create(stArray);
// or obj := SA([]);
a := obj.AsArray;
a.s[0] := 'aaaa';
a.s[1] := 'bbbb';
a.s[3] := 'cccc';
...
obj.SaveTo('File.txt');
a := nil; obj := nil;
...
end;
There is also the quote from help file: obj['foo[]'] := value; // add an item array
This suggests another way to populate an array (if the root object itself is not an array). Quoting http://code.google.com/p/superobject/source/browse/tests/test_usage.dpr
还有来自帮助文件的引用: obj['foo[]'] := value; // 添加一个项目数组
这暗示了另一种填充数组的方法(如果根对象本身不是数组)。引用http://code.google.com/p/superobject/source/browse/tests/test_usage.dpr
my_array := TSuperObject.Create(stArray);
my_array.I[''] := 1; // append
my_array.I[''] := 2; // append
my_array.I[''] := 3; // append
my_array.I['4'] := 5;
And later this object-array is inserted as property into yet another object
后来这个对象数组作为属性插入到另一个对象中
my_object := TSuperObject.Create(stObject);
my_object.I['abc'] := 12;
// my_object.S['path.to.foo[5]'] := 'bar';
my_object.B['bool0'] := false;
my_object.B['bool1'] := true;
my_object.S['baz'] := 'bang';
my_object.S['baz'] := 'fark';
my_object.AsObject.Delete('baz');
my_object['arr'] := my_array;
回答by Karina de Souza
A very good library for that is the LkJson: http://sourceforge.net/projects/lkjson/
一个非常好的库是 LkJson:http: //sourceforge.net/projects/lkjson/
To parse:
解析:
var jsText : String; jsObj : TlkJSONobject; begin
var jsText:字符串;jsObj : TlkJSONobject; 开始
jsObj:=TlkJSON.ParseText(jsText) as TlkJSONobject;
To convert it back into text:
要将其转换回文本:
jsText := TlkJSON.GenerateText(jsObj);

