解析天气预报JSON数据

JSON字符串

const
json2 = '{' + #13#10 +
'"error":0,' + #13#10 +
'"status":"success",'+ #13#10 +
'"date":"2014-03-04",'+ #13#10 +
'"results":'+ #13#10 +
'[{"currentCity":"成都",'+ #13#10 +
' "weather_data":['+ #13#10 +
'{'+ #13#10 +
'"date":"周二(今天, 实时:12℃)",'+ #13#10 +
'"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png",'+ #13#10 +
'"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",'+ #13#10 +
'"weather":"多云",'+ #13#10 +
'"wind":"北风微风",'+ #13#10 +
'"temperature":"15 ~ 6℃"'+ #13#10 +
'},'+ #13#10 +
'{'+ #13#10 +
'"date":"周三",'+ #13#10 +
'"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png",'+ #13#10 +
'"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",'+ #13#10 +
'"weather":"阴转小雨",'+ #13#10 +
'"wind":"北风微风",'+ #13#10 +
'"temperature":"14 ~ 7℃"'+ #13#10 +
'},'+ #13#10 +
'{'+ #13#10 +
'"date":"周四",'+ #13#10 +
'"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png",'+ #13#10 +
'"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",'+ #13#10 +
'"weather":"小雨",'+ #13#10 +
'"wind":"北风微风",'+ #13#10 +
'"temperature":"12 ~ 7℃"'+ #13#10 +
'},'+ #13#10 +
'{'+ #13#10 +
'"date":"周五",'+ #13#10 +
'"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png",'+ #13#10 +
'"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",'+ #13#10 +
'"weather":"小雨",'+ #13#10 +
'"wind":"南风微风",'+ #13#10 +
'"temperature":"9 ~ 6℃"'+ #13#10 +
'}'+ #13#10 +
']'+ #13#10 +
'}'+ #13#10 +
']}';

1)MORMOT SDK解析JSON:

uses
SynCommons;

procedure TForm1.Button5Click(Sender: TObject);
var
doc: variant;
json: RawUTF8;
count, i: Integer;
begin
doc := _JsonFast(JSON2); // json还原为variant
Memo1.Clear;
Memo1.Lines.Add(doc.error); // 0
Memo1.Lines.Add(doc.status); // success
Memo1.Lines.Add(doc.date); // 2014-03-04
Memo1.Lines.Add(doc.results._(0).currentCity); // 成都
count := doc.results._(0).weather_data._count; // 取JSON数组 长度
for i := 0 to count - 1 do // 遍历JSON数组
Memo1.Lines.Add(doc.results._(0).weather_data._(i).weather);
end;

2)DELPHI官方库解析JSON:

 procedure TfjsonDemo.Button1Click(Sender: TObject);
var
  root, results: TJSONObject;
  LItem: TJSONValue;
  weather: TJSONArray;
  StrJson: string;
  result: string;
  i: Integer;
begin
  StrJson := Memo1.Text;
  root := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(StrJson), 0) as TJSONObject;
  results := (root.GetValue('results') as TJSONArray).Get(0) as TJSONObject;
  weather := results.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;
  Memo2.Text := result;
end;

解析天气预报JSON数据的更多相关文章

  1. mormot解析天气预报JSON数据

    mormot解析天气预报JSON数据 uses SynCommons; constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'&qu ...

  2. Gson解析复杂Json数据

    背景                                                                   json是一种数据格式,便于数据传输.存储.交换. gson是 ...

  3. 模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。

    需要用到的lib包 :解析json  gson包,从网络地址解析json数据成String字符串的异步网络解析工具AsyncHttpClient等 下载地址:点击下载 Xlistview 下拉上拉第三 ...

  4. 解析网络json数据,模拟美团界面显示。

    <?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...

  5. Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据

    版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...

  6. C# JToken类的使用,实现解析动态json数据、遍历、查找

    在原来解析json数据是,一般都是用反序列化来实现json数据的解读,这需要首先知道json数据的结构并且建立相应的类才能反序列化,一旦遇到动态的json数据,这种方法就不使用. 为了解决动态解析js ...

  7. java android使用Gson解析泛型json数据

    那就直接开始吧. 在我们获取服务器返回的json数据有时候会出现这种情况,比如: {"body":{"attrName":"feed",&q ...

  8. Unity3d-XML文件数据解析&amp;JSON数据解析

    1.XML文件数据解析:(首先须要导入XMLParser解析器,The latest released download from:http://dev.grumpyferret.com/unity/ ...

  9. IOS开发--解析复杂json数据

    json的自我介绍:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人阅 ...

随机推荐

  1. csu 1556(快速幂)

    1556: Jerry's trouble Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 787  Solved: 317[Submit][Statu ...

  2. Git分支管理小结

    分支管理命令 每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD严格来说不是指向提交,而是指向mas ...

  3. 安装requests

    requests包让Python程序能够轻松地向网站请求信息以及检查返回的响应.要安装requests,请执行类似于下面的命令: $ pip3 install --user requests http ...

  4. python ConfigParser 配置读写

    我们写程序的时候一般都会写程序包的时候,很少硬编码写在一起,这有个很好的配置文件包. 参考 ConfigParser  和   ConfigParser 的使用 一.ConfigParser 简介 C ...

  5. d2i_xxx出错

    在生成DER编码是X509_ALGOR类型没有赋值导致,要先new,然后赋值. req_st->req.appKeyReq->appKeyType = X509_ALGOR_new(); ...

  6. 【转】python+django+vue搭建前后端分离项目

    https://www.cnblogs.com/zhixi/p/9996832.html 以前一直是做基于PHP或JAVA的前后端分离开发,最近跟着python风搭建了一个基于django的前后端分享 ...

  7. Python类总结-多态及鸭子类型

    Python天生支持多态. 什么是多态: 一类事务的多种形态. 多态的一个例子 class Alipay(): def pay(self,money): print('用支付宝支付了%s元' % mo ...

  8. 通过因特网连接Beaglebone Black

    通过因特网连接Beaglebone Black 通过网络连接,可以使你方便地从各种地方以及各种不同的电脑访问到Beaglebone Black.这种连接Beaglebone Black方式通常使用5V ...

  9. Tsinsen Palisection

    建回文树. 正反建统计一种前缀和求出所有不相交的,用总数减去就是答案数. 在这里我们可以知道一个字符串中所有回文串的个数即为num数组之和(因为以一个节点为回文串结尾的字串都是唯一的) 也可以是cnt ...

  10. Spring 注解大全与详解

    Spring使用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...