如何使用SuperObject lib在Delphi中创建JSON文件? [英] How to create JSON-file in Delphi using SuperObject lib?

查看:959
本文介绍了如何使用SuperObject lib在Delphi中创建JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Delphi 2010和 superobject 库。

I'm using Delphi 2010 and superobject library.

我已经明白了如何解析json文件,但是我没有想法如何创建json?

I have understand how to parse json-file, but I have no ideas how to create json?

该算法是:


  1. 在TStringGrid中解析JSON和加载

  2. 添加数据

  3. 将所有TStringGrid数据保存到json。

需要一些例子。

谢谢。

推荐答案

代码示例将以下结构提供给JSON对象,然后保存到文件:

Code sample to feed following structure to JSON object, then save to file:

(*
{
  "name": "Henri Gourvest", /* this is a comment */
  "vip": true,
  "telephones": ["000000000", "111111111111"],
  "age": 33,
  "size": 1.83,
  "adresses": [
    {
      "adress": "blabla",
      "city": "Metz",
      "pc": 57000
    },
    {
      "adress": "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;

这篇关于如何使用SuperObject lib在Delphi中创建JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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