• delphi XE3解析JSON数据


    测试数据如下:

    Memo1.text中的数据:

    {
    "date":"周二(今天, 实时:12℃)",
    "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png",
    "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",
    "weather":"多云",
    "wind":"北风微风",
    "temperature":"15 ~ 6℃"
    }

    Memo2.text中的数据:

    {
    "success":"true",
    "appId":"9",
    "data":[

    {
    "itemId":"536273259873","title":"u52A0u7ED2u52A0u539Au54FAu4E73u5916u51FAu536Bu8863"
    },
    {
    "itemId":"539867455759","title":"u5C0Fu7EA2u978Bu60C5u4FA3u8FD0u52A8u4F11u95F2u978Bu68C9u978B"
    }

    ]
    }

    ..............

    uses DBXJSON, DBXJSONCommon, DBXJSONReflect;

    ...............

    procedure TForm1.Button1Click(Sender: TObject);
    var
    JO: TJSONObject;
    StrJson: String;
    LJPair: TJSONPair;
    begin
    StrJson := Memo1.Text;
    JO := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson),
    0) as TJSONObject;

    ShowMessage(JO.Get('date').JsonValue.Value);
    ShowMessage(JO.Get(3).JsonValue.Value);

    for LJPair in JO do
    begin //遍历 {..} 中的数据
    ShowMessage(LJPair.JsonValue.Value);
    end;

    end;


    procedure TForm1.Button2Click(Sender: TObject);
    var
    JO,Jo2: TJSONObject;
    StrJson: String;
    LJPair: TJSONPair;
    elementCount: Integer;
    i: Integer;
    JA: TJSONArray;
    begin
    elementCount:=0;
    StrJson := Memo2.Text;
    JO := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson),
    0) as TJSONObject;

    ShowMessage((JO.Get('success').JsonValue as TJSONTrue).ToString);
    ShowMessage(JO.Get(1).JsonValue.Value);

    for LJPair in JO do
    begin //遍历 {..} 中的数据
    // ShowMessage(LJPair.JsonValue.Value);
    if LJPair.JsonValue is TJSONArray then
    begin
    JA:=LJPair.JsonValue as TJSONArray;
    elementCount:= JA.Size;
    showmessage(inttostr(elementCount));
    for i := 0 to elementCount-1 do
    begin
    Jo2:= JA.Get(i) as TJSONObject;
    if Jo2 is TJSONObject then
    begin
    ShowMessage(Jo2.Get('itemId').JsonValue.Value);
    ShowMessage(Jo2.Get('title').JsonValue.Value);

    end;

    end;
    end;
    end;

    end;

  • 相关阅读:
    java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä'.. 问题解决方法
    Mysql存储引擎federated
    实习心语
    Linux版本CentOS、Ubuntu和Debian的异同
    Ubuntu忘记MySQL密码重设方法
    运行时异常和一般异常
    网络爬虫-正方教务系统登录
    大四心语
    缓存更新的套路
    (String)、toString、String.valueOf的区别
  • 原文地址:https://www.cnblogs.com/yzryc/p/6026597.html
Copyright © 2020-2023  润新知