发布网友 发布时间:2022-04-21 16:53
共2个回答
热心网友 时间:2023-07-21 04:01
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.
热心网友 时间:2023-07-21 04:02
XE自带JSON解析或者直接用superobject JSON ,可以直接遍历JSON。
给你个Superjson的
2. JSON 分为两种 1种是直接读取的
例如 :RuleType
一种是数组: 可以循环读取:
例如:
"BarCodeRule": [
{
"Min": 2,
"Max": 2,
"SKind": 0,
"Kind": 0,
"Caption": "\u5c3a\u7801"
},
{
"Min": 2,
"Max": 2,
"SKind": 0,
"Kind": 0,
"Caption": "\u9c\u8272"
},
{
"Min": 2,
"Max": 2,
"SKind": 0,
"Kind": 0,
"Caption": "\u5382\u5bb6"
},
{
"Min": 2,
"Max": 2,
"SKind": 0,
"Kind": 0,
"Caption": "\u5e74\u5ea6"
}
],
一种是嵌套结构
例如:
"addresses":{"address":"A省B市", "pc":"100001"},
或者
"RuleDesc": {
"FBarCodeComboCount": 1000,
"LabBartCodeStru": "[\u5c3a\u7801][\u9c\u8272][\u5382\u5bb6][\u5e74\u5ea6]",
"FMaterialSize_Min": 8,
"FMaterialSize_Max": 8,
"FMaterialComboCount": 1000,
"LabMaterielStru": "[\u9c\u8272][\u5382\u5bb6]",
"LabMaterielNo": "\u624b\u52a8\u586b\u5199\u8d27\u53f7",
"lbBarCodeCombo_Caption": "",
"SerialNumber": "\u5ffd\u7565",
"lbMaterialComBo_Caption": "",
"lbBarCodeLen_Caption": "\u6761\u7801\u957f\u5ea6\uff1a\u6700\u5927(8)\/\u6700\u5c0f(8)\u6839\u636e\u6d41\u6c34\u53f7\u4f4d\u6570\uff0c\u5f53\u524d\u6761\u7801\u7684\u7ec4\u5408\u4e2a\u6570(1000)",
"lbMaterialLen_Caption": "\u81ea\u5b9a\u4e49\u7f16\u7801\u6a21\u5f0f",
"FBarCodeSize_Min": 8,
"FBarCodeSize_Max": 8
}
superobject读取JSON
procere TForm1.Button11Click(Sender: TObject);
var
jo:ISuperObject;
s:string;
ja: TSuperArray;
i:integer;
begin
//Memo2.Clear;
jo:=SO(Memo1.Lines.Text); //构建JSON数据
// s:=jo['name'].AsString;
// memo2.Lines.Add(s);
// ShowMessage(jo['addresses.address'].AsString); //内嵌对象访问
//
// ShowMessage(jo['RuleType'].AsString); //直接读取
// ShowMessage(jo['RuleDesc.lbBarCodeLen_Caption'].AsString); //读取内嵌对象
// ShowMessage(jo['RuleDesc.LabMaterielStru'].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);
//读取数组
ja := jo['BarCodeRule'].AsArray;
listbox3.Clear;
for I := 0 to ja.Length -1 do
begin
//ShowMessage(ja[i]['name'].AsString);
ListBox3.Items.Add(ja[i]['Caption'].AsString);
end;
end;