zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    http请求消息体和响应消息体
    整型常量
    C语言中字符串后面的'\0'
    String类
    二进制转成十六进制
    http消息头
    NULL和NUL
    拷贝构造函数和赋值表达式
    awk中的FS
    之前给女性网增加的一个滚动展示
  • 原文地址:https://www.cnblogs.com/yzryc/p/6026597.html
Copyright © 2011-2022 走看看