delphi自带的Json单元读取Json用MJson .GetValue<string>(‘path’)

delphi自带的Json单元读取Json还是特别方便的。如下:

{
    "date": "2014-03-04",
    "error": 0,
    "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℃

  FreeAndNil(MJson );
 end;
end;
posted @ 2020-12-23 15:25  沂水蓝海  阅读(578)  评论(0)    收藏  举报