JSON 之 SuperObject(3): 访问
测试数据提前加入 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): 访问的更多相关文章
- JSON 之 SuperObject(11): TSuperTableString、TSuperAvlEntry
JSON 之 SuperObject(11): TSuperTableString.TSuperAvlEntry - 万一 - 博客园http://www.cnblogs.com/del/archiv ...
- JSON 之 SuperObject(8): 关于乱码的几种情况 - 向 Henri Gourvest 大师报告
这几天学习 JSON - SuperObject, 非常幸运地得到了其作者 Henri Gourvest 大师的同步指点! (Henri 大师也是 DSPack 和 GDI+ 头文件的作者; 大师是法 ...
- JSON 之 SuperObject(6): 方法
SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...
- JSON 之 SuperObject(7): 可以省略的双引号
在 JSON 中, 字符串应该在双引号中; 从上个例子才发现: 原来这个双引号可以省略, 有空格都行 当然只是在程序代码中可以省略, 对象会自动识别添加的. 即如此, 下面写法都可以: uses Su ...
- JSON 之 SuperObject(5): Format 与转义字符
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- JSON 之 SuperObject(2): 构建方式与 AsJSon
SuperObject 构建一个 JSON 的常用方法: 从字符串.从文件.从流. unit Unit1; interface uses Windows, Messages, SysUtils, ...
- JSON 之 SuperObject(1)
一直盼着 Delphi 能够直接支持 "正则表达式" 与 "JSON"; Delphi 2009 刚来的时候, 有了 JSON, 但不好, 那时尝试过一点. 这 ...
- servlet 中处理 json 请求,并访问 service 类,返回处理结果
前言:jar 包中的 servlet 必须可以处理前端发出的 ajax 请求,接收参数,并返回结果. github地址:yuleGH github 这里有个约定,url 地址是 .json 结尾的,如 ...
- JSON 之 SuperObject(10): Merge、Clone、ForcePath
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
随机推荐
- vml 在IE8 不显示的问题, Group不能用等问题.
IE8 不显示的问题: <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> ...
- 100个经典的C算法
1.题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔 子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数 为多少? #include<stdio.h&g ...
- spark分片个数的确定及Spark内存错误(GC error)的迂回解决方式
我们知道,spark中每个分片都代表着一部分数据,那么分片数量如何被确认的呢? 首先我们使用最常见的HDFS+Spark,sparkDeploy的方式来讨论,spark读取HDFS数据使用的是spar ...
- 13test07;字符排序,去重,三三输出
#include<iostream> #include<string> using namespace std; void buddle(char*,int);//对输入字符的 ...
- post 方式提交XML文件调用接口
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java. ...
- 使用tomcat7创建异步servlet
该篇文章翻译自:http://developerlife.com/tutorials/?p=1437 一.简介 Servlet API 3.0 之前,需要使用类似Comet的方式来实现创建异步的Ser ...
- ExtJs之进度条实现
慢慢按书上的作. <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta h ...
- linux下crontab实现定时服务详解
http://www.jb51.net/LINUXjishu/151805.html 任务调度的crond常驻命令 crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动 ...
- lintcode :同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法
PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法 PHP5.3 split() 不建议使用的原因:PHP 5.3.0 之后的r ...