delphi怎么得到下面的json数据中的多个节点值

 我来答
龙氏风采
2017-11-15 · 知道合伙人互联网行家
龙氏风采
知道合伙人互联网行家
采纳数:5849 获赞数:12817
从事互联网运营推广,5年以上互联网运营推广经验,丰富的实战经

向TA提问 私信TA
展开全部
Delphi下解析json可以去下载一个superobject单元,关于这个单元的使用可以百度一下,很好用的

Delphi/Pascal code?

// Sample 2: how to get child type and subobject fields
//
// Leonid Koninin, 02/03/2007
program sample2;
{$APPTYPE CONSOLE}
uses SysUtils, uLkJSON in 'uLkJSON.pas';
var JSon, XJSon :TlkJSONobject; JSonNode :TlkJSONString; JSonNodeList :TlkJSONlist; Str :String; i :Integer;
begin
Str := '{"string1":"one", "string2":"two", '
+'"childobject" : {"objstr1" :"Oone", "objstr2" :"Otwo"},'
+'"childobject2":[{"obj2str1":"2one"},{"obj2str1":"2two"}]}'; writeln(Str);

JSon := TlkJSON.ParseText(Str) as TlkJSONobject; // restore object (parse text)

if not assigned(JSon) then begin // how to obtain type of child
writeln('error: xs not assigned!'); readln; //exit;
end else begin //Field[] is 方式判断类型
if JSon.Field['childobject'] is TlkJSONString then writeln('type: xs is string!'); //string
if JSon.Field['childobject'] is TlkJSONlist then writeln('type: xs is list!'); //list 多个Json子节点
if JSon.Field['childobject'] is TlkJSONobject then writeln('type: xs is object!'); //Oject 单个json子节点

//以下类型,实际不常用
if JSon.Field['childobject'] is TlkJSONnumber then writeln('type: xs is number!'); //数字 value前后不加引号
if JSon.Field['childobject'] is TlkJSONboolean then writeln('type: xs is boolean!'); //boolean
if JSon.Field['childobject'] is TlkJSONnull then writeln('type: xs is null!'); //Null 改为空格值
end;

//Filed[].SelfType 方式判断类型
case JSon.Field['childobject'].SelfType of //the other way (0.93+)
jsString :writeln('other type: xs is string');
jsObject :writeln('other type: xs is object');
jsList :writeln('other type: xs is list');

jsNumber :writeln('other type: xs is number');
jsBoolean :writeln('other type: xs is boolean');
jsNull :writeln('other type: xs is null');
jsBase :writeln('other type: xs is base');
end;
writeln('self-type name: ', JSon.Field['childobject'].SelfTypeName);

XJSon :=JSon.Field['childobject'] as TlkJSONobject; //JSON中,有下级节点的,不像xml那样称为节点对象,仍称为JSOn对象

//Field[] as方式取值,完美
JSonNode :=XJSon.Field['objstr1'] as TlkJSONString; //we know what xs chilren are strings
writeln(JSonNode.value);

JSonNode :=XJSon.Field['objstr2'] as TlkJSONstring;
writeln(JSonNode.value);

//getstring,快速
writeln(XJSon.getString('objstr1')); //new v0.99 +syntax!
writeln(XJSon.getString('objstr2')); readln;

JSon.Free;
end.
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式