设为首页 加入收藏

TOP

Xe7 System.Json解析数据格式(一)
2017-10-10 12:06:39 】 浏览:5784
Tags:Xe7 System.Json 解析 数据 格式
一、Demo一 解析嵌套数组
Json数据
{"code":1,"msg":"","data":{"GradeInfo":[{"gradeid":1,"gradename":"普通介绍人","graderate":0.02}],"UserList":[{"stype":"User","userid":"119110"},{"stype":"User","userid":"11911044"},{"stype":"User","userid":"119110444"},{"stype":"User","userid":"121121"},
{"stype":"User","userid":"13211111113"},{"stype":"User","userid":"abcadsfa"},{"stype":"Customer","userid":"admin"},{"stype":"Shoper","userid":"Shop2222"}]}}
View Code
var
 Jsobj:TJSONObject;
 i,j:Integer;
 Jarr:TJSONArray;
 temp:string;
 v:string;
 Jsvalue:Tjsonvalue ;

begin

    Jsobj:=TJSONObject.ParseJSONValue(Memo1.Text)  as TJSONObject;
    temp:=Jsobj.GetValue('data').ToString;
    Jsobj:=TJSONObject.ParseJSONValue(temp)  as TJSONObject;

    jarr := TJSONArray(Jsobj.GetValue('UserList'));
       for i := 0 to Jarr.Size-1 do
      begin
        v:=(Jarr.Items[i].ToString);
         Jsobj:=TJSONObject.ParseJSONValue(v)  as TJSONObject;
//         for j := 0 to Jsobj.Count do
//         begin
           ShowMessage(Jsobj.GetValue('userid').ToString);
         //end;
end;
end;
View Code
二 Demo二
Delphi XE5带了system.json单元,原生提供了json支持类。下面是解析json用法说明:
最简单的JSON大致像这样


{
"date":"周二(今天, 实时:12℃)",
"dayPictureUrl":"https://www.cppentry.com/upload_files/article/86/1_d2lrv__.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":"https://www.cppentry.com/upload_files/article/86/1_d2lrv__.png",
"weather":"多云",
"wind":"北风微风",
"temperature":"15 ~ 6℃"
},
{
"date":"周三",
"dayPictureUrl":"https://www.cppentry.com/upload_files/article/86/1_c1lrx__.png",
"weather":"阴转小雨",
"wind":"北风微风",
"temperature":"14 ~ 7℃"
},
{
"date":"周四",
"dayPictureUrl":"https://www.cppentry.com/upload_files/article/86/1_t2mpb__.png",
"weather":"小雨",
"wind":"北风微风",
"temperature":"12 ~ 7℃"
},
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Asphyre引擎】发布了新版本V101 下一篇delphi中string的管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目