Delphi 解析JSON  
官方Delphi 学习QQ群: 682628230(三千人)
频道

Delphi 解析JSON


Delphi 解析JSON


参考数据


{

    "五班": [

        {

            "姓名": "张三",

            "成绩": 75.5

        },

        {

            "姓名": "李四",

            "成绩": 21.7

        },

        {

            "姓名": "王五",

            "成绩": 75.52

        },

        {

            "姓名": "孙六",

            "成绩": 21.72

        }

    ],

    "六班": [

        {

            "姓名": "张三6",

            "成绩": 75.56

        },

        {

            "姓名": "李四6",

            "成绩": 21.76

        },

        {

            "姓名": "王五6",

            "成绩": 75.526

        },

        {

            "姓名": "孙六6",

            "成绩": 21.726

        }

    ]

}


记得声明


uses

  System.JSON,

  System.Generics.Collections;


相关代码


procedure TForm2.Button1Click(Sender: TObject);

var

  I: Integer;

  y_JsonStr: string;

  y_SubArray: TJSONArray;

  y_JsonObject: TJSONObject;

  y_SubJsonObj: TJSONObject;

begin

  // 读取JSON文件

  y_JsonStr := Trim(Memo1.Text);

  y_JsonObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(y_JsonStr), 0) as TJSONObject;

  // 取内层

  y_SubArray := y_JsonObject.getValue('五班') as TJSONArray;

  for I := 0 to y_SubArray.Count - 1 do

  begin

    y_SubJsonObj := y_SubArray.Get(I) as TJSONObject;

    // 第一个姓名为自定义的,第二个姓名位参数名【用来获取相关参数值】

    Memo2.Lines.Add(Format('标签:%s = %s', ['姓名', y_SubJsonObj.GetValue('姓名').ToString]));

    Memo2.Lines.Add(Format('标签:%s = %s', [y_SubJsonObj.Get(1).JsonString.ToString, y_SubJsonObj.GetValue('成绩').ToString]));

    Memo2.Lines.Add('--------------------------------');

  end;

end;

————————————————


原文链接:https://blog.csdn.net/qq_44111597/article/details/108077373



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.038563966751099 seconds