Delphi XE6 原生解析json


Delphi XE5帶了system.json單元,原生提供了json支持類。下面是解析json用法說明: 最簡單的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先判斷是不是數組。
原文地址
補充,在原文中,作者沒有提到,如何檢查一個指定的串值是否存在,比如下面這行代碼: weather := JSONObject.GetValue('weather_data'); 如果'weather_data'不存在,JSONObject.GetValue方法是要產生異常的,那么,該如何檢查weath_data是否存在呢?
先聲明一個 var   jsonvalue: Tjsonvalue; 然后,利用JSONObject.TryGetValue方法來檢查。     if jsonObject.TryGetValue('weather_data', jsonvalue) then ... 如果weath_data存在,可以進一步通過jsonvalue.value取出其值。 注意,這個jsonvalue不用建立與釋放。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM