游子日月长

笑渐不闻声渐悄,多情却被无情恼!

导航

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":"\u52A0\u7ED2\u52A0\u539A\u54FA\u4E73\u5916\u51FA\u536B\u8863"
},
{
"itemId":"539867455759","title":"\u5C0F\u7EA2\u978B\u60C5\u4FA3\u8FD0\u52A8\u4F11\u95F2\u978B\u68C9\u978B"
}

]
}

 

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

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;

 

posted on 2016-11-03 14:51  游子日月长  阅读(1173)  评论(0编辑  收藏  举报