测试数据提前加入 Memo1 中:



代码文件:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses SuperObject;

//数组式访问
procedure TForm1.Button1Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := SO(Memo1.Text);
  ShowMessage(jo['name'].AsString);
  ShowMessage(jo['age'].AsString);
  ShowMessage(jo['sex'].AsString);
  ShowMessage(jo['weight'].AsString);
  ShowMessage(jo['addresses'].AsString);
  ShowMessage(jo['tel'].AsString);
  ShowMessage(jo['children'].AsString);
end;

//指定返回类型
procedure TForm1.Button2Click(Sender: TObject);
var
  jo: ISuperObject;
  s: string;
  i: Integer;
  d: Double;
  b: Boolean;
begin
  jo := SO(Memo1.Text);

  s := jo['name'].AsString;
  i := jo['age'].AsInteger;
  b := jo['sex'].AsBoolean;
  d := jo['weight'].AsDouble;

  ShowMessage(s);
  ShowMessage(IntToStr(i));
  ShowMessage(BoolToStr(b, True));
  ShowMessage(FloatToStr(d));

  s := jo.S['name'];
  i := jo.I['age'];
  b := jo.B['sex'];
  d := jo.D['weight'];

  ShowMessage(s);
  ShowMessage(IntToStr(i));
  ShowMessage(BoolToStr(b, True));
  ShowMessage(FloatToStr(d));
{
  jo['name'] 是 jo.O['name'] 的简化形式, 因为它是类的默认数组;
  jo.N 类似 jo.O, 区别是 jo.N 在参数不妥时可返回空;
  jo.M 和方法有关, 对象中可嵌入方法.
}
end;

//内嵌对象访问
procedure TForm1.Button3Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := SO(Memo1.Text);

  ShowMessage(jo['addresses.address'].AsString);
  ShowMessage(jo['addresses.pc'].AsString);
end;

//TSuperArray
procedure TForm1.Button4Click(Sender: TObject);
var
  jo: ISuperObject;
  ja: TSuperArray;
begin
  jo := SO(Memo1.Text);
  ja := jo['tel'].AsArray;

  ShowMessage(IntToStr(ja.Length));
  ShowMessage(ja[].AsString);
  ShowMessage(ja[].AsString);

  ja := jo['children'].AsArray;
  ShowMessage(ja[]['name'].AsString);
  ShowMessage(ja[]['name'].AsString);
end;

//遍历对象
procedure TForm1.Button5Click(Sender: TObject);
var
  item,jo: ISuperObject;
begin
  jo := SO(Memo1.Text);
  for item in jo do ShowMessage(item.AsJSon(False, False));
end;

//遍历子对象
procedure TForm1.Button6Click(Sender: TObject);
var
  item,jo: ISuperObject;
begin
  jo := SO(Memo1.Text);
  for item in jo['children'] do
    ShowMessage(item.AsJSon(False, False));
end;

//使用 TSuperAvlEntry 或 TSuperTableString 遍历
procedure TForm1.Button7Click(Sender: TObject);
var
  jo: ISuperObject;
  item: TSuperAvlEntry;
begin
  jo := SO(Memo1.Text);
  for item in jo.AsObject do
    ShowMessageFmt('Name: %s; Value: %s', [item.Name, item.Value.AsString]);
end;

//使用 TSuperObjectIter 遍历
procedure TForm1.Button8Click(Sender: TObject);
var
  jo: ISuperObject;
  item: TSuperObjectIter;
begin
  jo := SO(Memo1.Text);

  if ObjectFindFirst(jo, item) then
  repeat
    ShowMessageFmt('key: %s; val: %s', [item.key, item.val.AsString]);
  until not ObjectFindNext(item);
  ObjectFindClose(item);
end;

end.

窗体文件:


object Form1: TForm1
  Left =
  Top =
  Caption = 'Form1'
  ClientHeight =
  ClientWidth =
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch =
  TextHeight =
  object Memo1: TMemo
    Left =
    Top =
    Width =
    Height =
    Align = alLeft
    Lines.Strings = (
      '{'
      '  "name": "'#24352#19977'", /* '#27880#37322' */'
      '  "age": 33,'
      '  "sex": true,'
      '  "weight": 123.456,'
      '  "tel": ["86-1111111", "86-2222222"],'
      '  "addresses":{"address":"A'#30465'B'#24066'", "pc":"100001"},'
      '  "children": ['
      '    {'
      '      "name": "'#24352#32487'",'
      '      "age": "22",'
      '      "sex": true'
      '    },'
      '    {'
      '      "name": "'#24352#25215'",'
      '      "age": "11",'
      '      "sex": false'
      '    }'
      '  ]'
      '}')
    ScrollBars = ssBoth
    TabOrder =
    ExplicitHeight =
  end
  object Button1: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button1'
    TabOrder =
    OnClick = Button1Click
  end
  object Button2: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button2'
    TabOrder =
    OnClick = Button2Click
  end
  object Button3: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button3'
    TabOrder =
    OnClick = Button3Click
  end
  object Button4: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button4'
    TabOrder =
    OnClick = Button4Click
  end
  object Button5: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button5'
    TabOrder =
    OnClick = Button5Click
  end
  object Button6: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button6'
    TabOrder =
    OnClick = Button6Click
  end
  object Button7: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button7'
    TabOrder =
    OnClick = Button7Click
  end
  object Button8: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = 'Button8'
    TabOrder =
    OnClick = Button8Click
  end
end

JSON 之 SuperObject(3): 访问的更多相关文章

  1. JSON 之 SuperObject(11): TSuperTableString、TSuperAvlEntry

    JSON 之 SuperObject(11): TSuperTableString.TSuperAvlEntry - 万一 - 博客园http://www.cnblogs.com/del/archiv ...

  2. JSON 之 SuperObject(8): 关于乱码的几种情况 - 向 Henri Gourvest 大师报告

    这几天学习 JSON - SuperObject, 非常幸运地得到了其作者 Henri Gourvest 大师的同步指点! (Henri 大师也是 DSPack 和 GDI+ 头文件的作者; 大师是法 ...

  3. JSON 之 SuperObject(6): 方法

    SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...

  4. JSON 之 SuperObject(7): 可以省略的双引号

    在 JSON 中, 字符串应该在双引号中; 从上个例子才发现: 原来这个双引号可以省略, 有空格都行 当然只是在程序代码中可以省略, 对象会自动识别添加的. 即如此, 下面写法都可以: uses Su ...

  5. JSON 之 SuperObject(5): Format 与转义字符

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  6. JSON 之 SuperObject(2): 构建方式与 AsJSon

    SuperObject 构建一个 JSON 的常用方法: 从字符串.从文件.从流. unit Unit1; interface uses   Windows, Messages, SysUtils, ...

  7. JSON 之 SuperObject(1)

    一直盼着 Delphi 能够直接支持 "正则表达式" 与 "JSON"; Delphi 2009 刚来的时候, 有了 JSON, 但不好, 那时尝试过一点. 这 ...

  8. servlet 中处理 json 请求,并访问 service 类,返回处理结果

    前言:jar 包中的 servlet 必须可以处理前端发出的 ajax 请求,接收参数,并返回结果. github地址:yuleGH github 这里有个约定,url 地址是 .json 结尾的,如 ...

  9. JSON 之 SuperObject(10): Merge、Clone、ForcePath

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

随机推荐

  1. Codeforces Round #204 (Div. 2)->D. Jeff and Furik

    D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. 《EnterLib PIAB深入剖析》系列博文汇总_转

    转: http://www.cnblogs.com/artech/archive/2008/08/08/1263418.html

  3. 2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls

    传说的SB DP: 题目 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...

  4. Css 单图片按钮实例(css 图片变换)

    1.场景描述,根据鼠标的移动,动态的切换按钮图片. 2.方法1,准备两张120*41的图片,一张正常状态图片,一张按下效果图片.在鼠标放在的按钮上设置按下图片,移开又恢复到正常状态图片.缺点:在网页上 ...

  5. linux权威指南 简记

    /proc 目录,linxu系统以文件形式存放进程信息,这是一个虚拟的文件系统,不占有任何磁盘空间,当读取该文件系统时,系统内核会拦截动作,并动态产生文件与目录的内容 查看该文件夹,会发现很多已数字命 ...

  6. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...

  7. java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片

    package com.lp.ecjtu; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...

  8. com组件的注册

    错误: 检索 COM 类工厂中 CLSID 为 {79AD7B73-C515-40B4-8B02-CB0F5FA5A1A8} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 ...

  9. Codeigniter 利用加密Key(密钥)的对象注入漏洞

    http://drops.wooyun.org/papers/1449 原文链接:http://www.mehmetince.net/codeigniter-object-injection-vuln ...

  10. ssh超时断开的解决方法

    当用SSH Secure Shell连接Linux时,如果几分钟没有任何操作,连接就会断开,必须重新登陆才行,每次都重复相同的操作,很是烦人,本文总结了两种解决的方法. 方法1:更改ssh服务器的配置 ...