JSON

delphi xe5 delphi 解析 json

字号+ 作者:H5之家 来源:H5之家 2015-11-21 16:43 我要评论( )

最简单的JSON大致像这样{date:周二(今天,实时:12℃),dayPictureUrl:http://api.map.baidu.com/images/weather/day/duoyun.png,nightPictureUrl:http://api.map.


最简单的JSON大致像这样
{
          "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℃"
        }
对于这种格式比较简单的json,解析是非常容易的

  StrJson := RESTResponse1.Content;
  JSONObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson), 0)
    as TJSONObject;


JSONObject.getValue('date'); 就可以得到date的值。如果像下面的这样结构比较复杂的json,就需要首先分析清楚这个json的格式才能获取成功。
{
  "error":0,
  "status":"success",
  "date":"2014-03-04",
  "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",
          "weather":"多云",
          "wind":"北风微风",
          "temperature":"15 ~ 6℃"
        }
,
        
        {
          "date":"周三",
          "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png",
          "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",
          "weather":"阴转小雨",
          "wind":"北风微风",
          "temperature":"14 ~ 7℃"
        }
,
        
        {
          "date":"周四",
          "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png",
          "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",
          "weather":"小雨",
          "wind":"北风微风",
          "temperature":"12 ~ 7℃"
        }
,
        
        {
          "date":"周五",
          "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png",
          "nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",
          "weather":"小雨",
          "wind":"南风微风",
          "temperature":"9 ~ 6℃"
        }
      ]
    }
  ]
}



这是一个嵌套结构,最外层是一个记录,包含"error", "status", "date",  "results"四个字段,前三个都是简单的键值对,而“results”是一个数组,目前只有一个元素,即一条记录,这条记录的字段是"currentCity"和"weather_data",再进一步"weather_data"又是一个组数,它有4个元素或者记录,每条记录里包含          "date", "dayPictureUrl","nightPictureUrl", "weather","wind", "temperature"字段。

要想取出里面的"weather_data",利用目前的DBXJSON里的TJSONObject是不能直接取出来的,例如这样

  StrJson := RESTResponse1.Content;
  JSONObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson), 0)
    as TJSONObject;

  weather := JSONObject.GetValue('weather_data'); 

需要一步一步的走,由于最外面是一个简单的json,可以先取出results,然后再取weather_data。


var
  JSONObject: TJSONObject;
  LItem: TJSONValue;
  LJPair: TJSONPair;
  weather: TJSONArray;
  StrJson: String;
  result: String;
  i: Integer;
begin
  StrJson := 'xxxxxxx';//假定是上面那个json
  JSONObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson), 0)
    as TJSONObject;
  JSONObject := (JSONObject.GetValue('results') as TJSONArray).Get(0)
    as TJSONObject;
  weather := JSONObject.GetValue('weather_data') as TJSONArray;

  for i := 0 to weather.size - 1 do //应该是4条记录
  begin
    LItem := (weather.Get(i) as TJSONObject).GetValue('weather'); //得到weather的值
    result := result + '|' + LItem.Value;
  end;
end
      
这段代码只是为了说明使用方法,没有做类型检查,最好在进行类型转换之前用is TJSONArray先判断是不是数组。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • php CI 实战教程:[5]用curl获取json并解析

    php CI 实战教程:[5]用curl获取json并解析

    2016-02-26 17:00

  •  JSON入门级学习总结-JSON数据结构

    JSON入门级学习总结-JSON数据结构

    2016-02-25 11:05

  • Android解析Json速度最快的库:json

    Android解析Json速度最快的库:json

    2016-02-13 18:00

  • JavaScript转换与解析JSON方法实例详解第1/2页

    JavaScript转换与解析JSON方法实例详解第1/2页

    2016-02-10 21:25

网友点评
t