文档章节

JSON 之 SuperObject(3): 访问

涂孟超
 涂孟超
发布于 2014/09/26 15:37
字数 762
阅读 29
收藏 0

测试数据提前加入 Memo1 中:
{ "name": "张三", /* 注释 */ "age": 33, "sex": true, "weight": 123.456, "tel": ["86-1111111", "86-2222222"], "addresses":{"address":"A省B市", "pc":"100001"}, "children": [ { "name": "张继", "age": "22", "sex": true }, { "name": "张承", "age": "11", "sex": false } ] }

代码文件:
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[0].AsString);
  ShowMessage(ja[1].AsString);

  ja := jo['children'].AsArray;
  ShowMessage(ja[0]['name'].AsString);
  ShowMessage(ja[1]['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 = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 296
  ClientWidth = 377
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 281
    Height = 296
    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 = 0
    ExplicitHeight = 292
  end
  object Button1: TButton
    Left = 287
    Top = 24
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 287
    Top = 55
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 2
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 287
    Top = 86
    Width = 75
    Height = 25
    Caption = 'Button3'
    TabOrder = 3
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 287
    Top = 117
    Width = 75
    Height = 25
    Caption = 'Button4'
    TabOrder = 4
    OnClick = Button4Click
  end
  object Button5: TButton
    Left = 287
    Top = 148
    Width = 75
    Height = 25
    Caption = 'Button5'
    TabOrder = 5
    OnClick = Button5Click
  end
  object Button6: TButton
    Left = 287
    Top = 179
    Width = 75
    Height = 25
    Caption = 'Button6'
    TabOrder = 6
    OnClick = Button6Click
  end
  object Button7: TButton
    Left = 287
    Top = 213
    Width = 75
    Height = 25
    Caption = 'Button7'
    TabOrder = 7
    OnClick = Button7Click
  end
  object Button8: TButton
    Left = 287
    Top = 244
    Width = 75
    Height = 25
    Caption = 'Button8'
    TabOrder = 8
    OnClick = Button8Click
  end
end

 
 
 
 
 

 

 

  

本文转载自:http://www.cnblogs.com/del/archive/2009/10/23/1588690.html

共有 人打赏支持
涂孟超
粉丝 12
博文 2011
码字总数 14107
作品 0
深圳
程序员
私信 提问
JSon SuperObject 研究2:数据集与JSON对象互转

JSon SuperObject 研究2:数据集与JSON对象互转 JSON不能完全替代XML,但绝对是未来的大势所趋,其优点是简单、体积小、解析更快、解析占用资源更少。在delphi中,数据集是最常用数据存取方式...

vga
2014/12/20
0
1
js中this的总结(转载)

在面向对象编程语言中,对于this关键字我们是非常熟悉的。比如C++、C#和Java等都提供了这个关键字,虽然在开始学习的时候觉得比较难,但只要理解了,用起来是非常方便和意义确定的。JavaScr...

xiaoqiang
2010/05/30
35
0
superobject 序列 数据集 之间转化

superobject 序列数据集 unit uDBJson; interface {$HINTS OFF} usesSysUtils, Classes, Variants, DB, DBClient, SuperObject; typeTTableJSon = class private const const const const co......

vga
2016/08/31
18
0
Delphi开源组件库--QDAC

QDAC 是由 QDAC 开发组为大家提供的一套 Delphi/C++ Builder 跨平台开源组件库,名称取自英文 Quick Data Access Components 的首字母。它支持 Delphi/C++ Builder 2007 以上的版本的开发环境...

swish
2015/11/23
1K
0
JS 操作 JSON完成动态指定访问值。求思路~

现在有一个json [{'id':'1', 'name':'name1'}, {'id':'2', 'name':'name2'}, {'id':'3', 'name':'name3'}, {'id':'4', 'name':'name4'}] 而我想完成的功能是,写一个function。如传入id,就访......

josh1707
2013/05/17
2K
5

没有更多内容

加载失败,请刷新页面

加载更多

Spring源码学习笔记-1-Resource

打算补下基础,学习下Spring源码,参考书籍是《Spring源码深度解析》,使用版本是Spring 3.2.x,本来想试图用脑图记录的,发现代码部分不好贴,还是作罢,这里只大略记录下想法,不写太细了 ...

zypy333
今天
10
0
RestClientUtil和ConfigRestClientUtil区别说明

RestClientUtil directly executes the DSL defined in the code. ConfigRestClientUtil gets the DSL defined in the configuration file by the DSL name and executes it. RestClientUtil......

bboss
今天
17
0

中国龙-扬科
昨天
2
0
Linux系统设置全局的默认网络代理

更改全局配置文件/etc/profile all_proxy="all_proxy=socks://rahowviahva.ml:80/"ftp_proxy="ftp_proxy=http://rahowviahva.ml:80/"http_proxy="http_proxy=http://rahowviahva.ml:80/"......

临江仙卜算子
昨天
10
0
java框架学习日志-6(bean作用域和自动装配)

本章补充bean的作用域和自动装配 bean作用域 之前提到可以用scope来设置单例模式 <bean id="type" class="cn.dota2.tpye.Type" scope="singleton"></bean> 除此之外还有几种用法 singleton:......

白话
昨天
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部