Delphi json

delphi的开源json库:superobject,用法简介 


先来看看什么是Json

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#,Java, Delphi,JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。

http://www.json.org/json-zh.html中,已经提供了针对各种语言的json支持。

从它名字中就可以看出,json最早是来源于javascript,在javascript中操作json数据异常简单清晰。可以像操作类对象一样运用json数据,如

//example1:

jdata ={"name":"老王","age":"25"};

str1 = jdata.name;//老王

str2 = jdata["name"];//老王

//example2:

jdata={"name":"老王","age":"25","address":{"address1":"福州","address2":"厦门"}};

str1 = jdata.address.address1;//福州

str2 = jdata["address"]["address1"];//福州

 

在Delphi2009之后的版本已经加入了对json的支持,不再需要第三方类库的支持。但我跟喜欢用HenriGourvest的开发的SuperObject,有兴趣的可以到http://www.progdigy.com/?page_id=6了解,最新版是1.24.

SuperObject提供了两个类库文件SuperObject.pas,superxmlparser.pas,其中SuperObject.pas就是用于操作json数据,而superxmlparser.pas用与从XML数据中解析出json数据。

简单例子

uses SuperObject;

procedure TForm1.Button1Click(Sender: TObject);
var
  soTest:ISuperObject;
begin
  soTest :=so('{"name":"张三","age":"25","address":{"Address1":"福州","address2":"厦门"}}');

  //等价的方法 soTest :=TSuperObject.ParseString('{"name":"张三","age":"25","address":{"Address1":"福州","address2":"厦门"}}');

  ShowMessage(soTest['name'].AsString);//张三

 ShowMessage(soTest['address.Address1'].AsString);//福州
end;
SuperObject类库提供了类似javascript操作json的数据的方式,相当方便。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
DELPHI彫和JSON代码* Copyright (c) 2006,2007,2008,2009 Leonid Koninin * leon_kon@users.sourceforge.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Leonid Koninin ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL Leonid Koninin BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值