Delphi XE JSON[3] 解析数据

{该文首发于博客园 滔Roy,无须授权即可转发,请自觉保留头部申明}

Delphi XE JSON[3] 解析数据

1、解析单个文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var
  JSONValue: TJSONValue;
  JSObject:TJSONObject;
  JValue:string;
begin
  if Trim(Memo1.Text)='' then Exit;
  JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
  JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
 
 
  if JSObject.TryGetValue(EditName.Text,JValue) then
    MemoValue.Text:=JValue;
 
  JSObject.Free;
end;

        

2、解析数据(指定数组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var
  JSONValue: TJSONValue;
  JSObject:TJSONObject;
  JSONArr:TJSONArray;
begin
  if Trim(Memo1.Text)='' then Exit;
  JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
  JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
 
  if JSObject.TryGetValue(EditName.Text,JSONArr) then  // JSONArr:=JSObject.GetValue(EditName.Text) as TJSONArray;
  MemoValue.Text:=JSONArr.ToString;
 
  JSObject.Free;
end;

3、循环解析数据(指定数组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var
  JSONValue: TJSONValue;
  JSObject:TJSONObject;
  JSONArr:TJSONArray;
  i:Integer;
  JValue:string;
begin
  if Trim(Memo1.Text)='' then Exit;
  MemoValue.Clear;
  JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
  JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
 
  if JSObject.TryGetValue(EditName.Text,JSONArr) then  // JSONArr:=JSObject.GetValue(EditName.Text) as TJSONArray;
  for i:=0 to JSONArr.Count-1 do begin
    if JSONArr.Items[i].TryGetValue('名称',JValue) then
    MemoValue.Lines.Add( '名称=' + JValue);
    if JSONArr.Items[i].TryGetValue('数量',JValue) then
    MemoValue.Lines.Add( '数量=' + JValue);
    if JSONArr.Items[i].TryGetValue('价格',JValue) then
    MemoValue.Lines.Add( '价格=' + JValue);
  end;
 
  JSObject.Free;
 
end;

4、循环格式化(不指定数组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var
  JSONValue: TJSONValue;
  JSObject,JSObject1:TJSONObject;
  JSONArr:TJSONArray;
  i,j,k:Integer;
  JName,JName1:string;
  JValue,JValue1:TJSONValue;
begin
  if Trim(Memo1.Text)='' then Exit;
  MemoValue.Clear;
  JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
  JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
 
  //第一层
  for i:=0 to JSObject.Count-1 do begin
    JName:=JSObject.Get(i).JsonString.ToString;
    JValue:=JSObject.Get(i).JsonValue;
    MemoValue.Lines.Add(JName+'=');
 
    if JValue is TJSONArray then begin   //先判断类型
      JSONArr:=JSObject.Get(i).JsonValue as TJSONArray;
      for j := 0 to JSONArr.Size-1 do begin
        JSObject1:=JSONArr.Items[j] as TJSONObject;
        for k := 0 to JSObject1.Count-1 do begin
          JName1:=JSObject1.Get(k).JsonString.ToString;
          JValue1:=JSObject1.Get(k).JsonValue;
          MemoValue.Lines.Add('  '+JName1+'='+JValue1.ToString);
        end;
      end;
    end else begin
      MemoValue.Lines.Add(JValue.ToString);
    end;
 
  end;
 
  JSObject.Free;
 
end;

 

 

  

 

 

 

创建时间:2022.03.20  更新时间:

posted on   滔Roy  阅读(734)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
历史上的今天:
2020-03-20 Delphi TListview[3]实现拖拽功能
2020-03-20 Delphi TListview[2] 常用方法和技巧
2020-03-20 Delphi TListview[1]功能及常用属性介绍

导航

点击右上角即可分享
微信分享提示