Delphi10.3读取JSON数据


本文整理自网络,侵删。

 
一、我们有一段JSON数据如下:

复制代码
{
    "五班": [
        {
            "姓名": "张三",
            "成绩": 75.5
        },
        {
            "姓名": "李四",
            "成绩": 21.7
        }
    ]
}
复制代码
二、使用Delphi代码读取,代码如下:

复制代码
uses
  System.Types,
  System.JSON,
  System.JSON.Types,
  System.JSON.Writers,
  System.JSON.Builders;

procedure TForm1.Button4Click(Sender: TObject);
var
  I: Integer;
  m_JsonStr: string;
  m_SubArray: TJSONArray;
  m_JsonObject: TJSONObject;
  m_SubJsonObj: TJSONObject;
begin
  // 读取JSON文件
  m_JsonStr := Trim(Memo1.Text);
  m_JsonObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(m_JsonStr), 0) as TJSONObject;
  
  // 取最外层
  for I := 0 to m_JsonObject.count - 1 do
  begin
    Memo2.Lines.Add(m_JsonObject.Get(I).JsonString.toString + ' = ' + m_JsonObject.Get(I).JsonValue.ToString);
  end;

  // 取内层
  m_SubArray := m_JsonObject.getValue('五班') as TJSONArray;
  for I := 0 to m_SubArray.size - 1 do
  begin
    m_SubJsonObj := m_SubArray.Get(I) as TJSONObject;
    Memo2.Lines.Add(Format('标签:%s = %s', [m_SubJsonObj.Get(0).JsonString.ToString, m_SubJsonObj.Get(0).JsonValue.ToString]));
    Memo2.Lines.Add(Format('标签:%s = %s', [m_SubJsonObj.Get(1).JsonString.ToString, m_SubJsonObj.Get(1).JsonValue.ToString]));
  end;
end;

相关阅读 >>

Delphi 利用hook api函数openprocess与terminateprocess来防止任务管理器结束进程

Delphi opendialog文件过滤类型

Delphi hex --> string

floattostr问题 保留小数位

Delphi 对比时间的函数

Delphi文件捆绑器

windows关机函数exitwindowsex使用大全(适用windows所有操作平台)

Delphi编程之关闭所有qq进程

Delphi 透明窗体

Delphi xe7 取得进程占用内存的两个函数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...