delphi自带的Json单元读取Json还是特别方便的。如下:
{
"date": "2014-03-14",
"error": 88,
"results": [
{
"currentCity": "成都",
"weather_data": [
{
"date": "周二(今天, 实时:12℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
"temperature": "15 ~ 6℃",
"weather": "多云",
"wind": "北风微风"
},
{
"date": "周三",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/yin.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",
"temperature": "14 ~ 7℃",
"weather": "阴转小雨",
"wind": "北风微风"
},
{
"date": "周四",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/xiaoyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",
"temperature": "12 ~ 7℃",
"weather": "小雨",
"wind": "北风微风"
},
{
"date": "周五",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/xiaoyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",
"temperature": "9 ~ 6℃",
"weather": "小雨",
"wind": "南风微风"
}
]
}
],
"status": "success"
}
Delphi 程序如下:
uses
System.JSON
var
MJsonTxt:string;
MJson:TJSONObject ;
Value,Value2:string;
begin
MJsonTxt :=Memo1.Lines .Text ;
MJson :=TJSONObject .ParseJSONValue(MJsonTxt )as TJSONObject ;
if Assigned(MJson ) then
begin
Value :=MJson .GetValue<string>('results[0].weather_data[1].temperature');
ShowMessage(Value ); //14 ~ 7℃
Value := MJson.GetValue<string>('results[0].weather_data[2].weather');
ShowMessage(Value ); // 小雨
Value := MJson.GetValue<string>('date');
ShowMessage(Value ); // 2014-03-14
Value := MJson.GetValue<string>('error'); // 整数可以按字符串取出
ShowMessage(Value ); // 88
Value := inttostr(MJson.GetValue<Integer>('error')); // 也可按整数取出
ShowMessage(Value ); // 88
FreeAndNil(MJson );
end;
end;
来源:oschina
链接:https://my.oschina.net/u/582827/blog/4836767