设为首页 加入收藏

TOP

Xe7 System.Json解析数据格式(二)
2017-10-10 12:06:39 】 浏览:5790
Tags:Xe7 System.Json 解析 数据 格式
{ "date":"周五", "dayPictureUrl":"https://www.cppentry.com/upload_files/article/86/1_t2mpb__.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不用建立与释放。 2014-11-19 网友发现上文中可能遇到的json串码问题,并给出了解决代码,Delphi <wbr>XE6 <wbr>原生解析jsonDelphi <wbr>XE6 <wbr>原生解析json! procedure TForm1.Button2Click(Sender: TObject); var LJsonArr : TJSONArray; LJsonValue : TJSONValue; LItem : TJSONValue; StrJson,S :string; begin StrJson := RESTresponse1.Content; LJsonArr := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson),0) as TJSONArray; for LJsonValue in LJsonArr do begin for LItem in TJSONArray(LJsonValue) do S :=Format('%s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]); end; end;

三、Demo三
{
  功能:DelphiXE7中使用JSON
  ------------------------------------------------------------------------------
  说明:
  1,使用Delphi自己带的JSON(system.json)。
  2,这仅仅是一个简单例子,以后还会增加演示功能。
  ------------------------------------------------------------------------------
  注意:
  1,JSON类创建后,里面所有元素不用管释放,JSON类自己管理,千万不要画蛇添足啊!!!!!!
  ------------------------------------------------------------------------------
  作者:孙玉良 QQ:14667479 Email:sunylat@163.com  修改时间:2014/11/23 00:13
  --------------------
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Asphyre引擎】发布了新版本V101 下一篇delphi中string的管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目