文档章节

JSON 之 SuperObject(3): 访问

涂孟超
 涂孟超
发布于 2014/09/26 15:37
字数 762
阅读 26
收藏 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
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
Mesos+Zookeeper+Marathon的Docker管理平台部署记录(2)--负载均衡marathon-lb

之前介绍了Mesos+Zookeeper+Marathon的Docker管理平台部署记录(1)的操作,多余的废话不说了,下面接着说下在该集群环境下的负载均衡marathon-lb的部署过程: 配置过程如下: 1)首先分别在...

吞吞吐吐的
2017/10/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

SpringData JPA 在解析实体类字段时驼峰自动添加下划线问题

SpringData JPA 使用的默认命名策略是: ImprovedNamingStrategy 。用下划线转换驼峰名,如 authorId ,转换成 author_id 。这样就遇到了一个问题:实体中驼峰命名的列名转换成下划线后,在M...

Jacktanger
14分钟前
0
0
Android JNI开发系列(十)JNI访问 Java 实例变量和静态变量

JNI访问 Java 实例变量和静态变量 Java 中的实例变量和静态变量,在本地代码中如何来访问和修改。静态变量也称为类变量(属性),在所有实例对象中共享同一份数据,可以直接通过类名.变量名来...

蔡小鹏
19分钟前
0
0
jsapi4加载的首个图层的范围被默认作为地图范围,且不能修改的解决

在map加载的第一个图层的图层范围(fullExtent),会被默认设置为map的全图范围,且不能更改,从一般地图控件角度来说,应该有fullExtent属性,作为地图的全图范围,但很遗憾jsapi4.9还没有 ...

canneljls
20分钟前
0
0
JSON.stringify()

JSON.parse()与JSON.stringify()的区别 JSON.parse()【从一个字符串中解析出json对象】 例子: //定义一个字符串 var data='{"name":"goatling"}' //解析对象 JSON.parse(data) 结果是: na...

废柴
20分钟前
0
0
HashSet

前言 Set的实现类都是基于Map来实现的(HashSet是通过HashMap实现的)。 构造图如下: 蓝色线条:继承 绿色线条:接口实现 正文 对于HashSet而言,它是基于HashMap来实现的,底层采用HashMap来保...

狼王黄师傅
22分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部