delphi json生成和解析
delphi json生成和解析
使用DELPHI 自带的JSON类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | { "date" : "2014-03-04" , "error" : 0, "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" , "temperature" : "15 ~ 6℃" , "weather" : "多云" , "wind" : "北风微风" }, { "date" : "周三" , "dayPictureUrl" : "http://api.map.baidu.com/images/weather/day/yin.png" , "nightPictureUrl" : "http://api.map.baidu.com/images/weather/night/xiaoyu.png" , "temperature" : "14 ~ 7℃" , "weather" : "阴转小雨" , "wind" : "北风微风" }, { "date" : "周四" , "dayPictureUrl" : "http://api.map.baidu.com/images/weather/day/xiaoyu.png" , "nightPictureUrl" : "http://api.map.baidu.com/images/weather/night/xiaoyu.png" , "temperature" : "12 ~ 7℃" , "weather" : "小雨" , "wind" : "北风微风" }, { "date" : "周五" , "dayPictureUrl" : "http://api.map.baidu.com/images/weather/day/xiaoyu.png" , "nightPictureUrl" : "http://api.map.baidu.com/images/weather/night/xiaoyu.png" , "temperature" : "9 ~ 6℃" , "weather" : "小雨" , "wind" : "南风微风" } ] } ], "status" : "success" } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | uses System.JSON var MJsonTxt: string ; MJson:TJSONObject ; Value,Value2: string ; begin MJsonTxt :=Memo1.Lines .Text ; MJson :=TJSONObject .ParseJSONValue(MJsonTxt ) as TJSONObject ; if Assigned(MJson ) then begin Value :=MJson .GetValue< string >( 'results[0].weather_data[1].temperature' ); ShowMessage(Value ); //14 ~ 7℃ FreeAndNil(MJson ); end; end; |
1 2 3 4 5 6 7 8 9 10 | var jr:tjsonarray; jr := jo.GetValue<TJSONArray>( 'data.dataList' ) ; for i := 0 to jr.Count - 1 do begin jrow := jr.Get(i) as TJSONObject; self.Caption:= jrow.GetValue< string >( 'ampm' ); end; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | uses System.JSON; procedure TForm1.Button1Click(Sender: TObject); //解析JSON begin var jo: TJSONObject := TJSONObject.ParseJSONValue( '{"name":"张三", "other":["中国","程序员"]}' ) as TJSONObject; //从字符串生成JSON Memo2.Lines.Add( '遍历JSON数据:' ); Memo2.Lines.Add( 'JSON数据数量:' + IntToStr(jo.Count)); var tmp: string ; for var i: integer := 0 to jo.Count - 1 do //1,遍历JSON数据 tmp := tmp + jo.Get(i).ToString; Memo2.Lines.Add(tmp); Memo2.Lines.Add( '' ); Memo2.Lines.Add( '按元素解析JSON数据:' ); //2,按元素解析JSON数据 tmp := 'name = ' + jo.Values[ 'name' ].value; Memo2.Lines.Add(tmp); var ja: TJSONArray := TJSONArray(jo.GetValue( 'other' )); // json数组 tmp := 'other = ' + jo.GetValue( 'other' ).ToString + #13#10; // 得到JSON数组字符串 for var i: integer := 0 to ja.Size - 1 do // 循环取得JSON数组中每个元素 tmp := tmp + IntToStr(i + 1) + ' : ' + ja.Items[i].Value + #13#10; Memo2.Lines.Add(tmp); jo.Free; end; procedure TForm1.Button2Click(Sender: TObject); //生成JSON begin var jo: TJSONObject := TJSONObject.Create; jo.AddPair( 'name' , '张三' ); // var ja: TJSONArray := TJSONObject.ParseJSONValue('["中国","程序员"]') as TJSONArray; var ja: TJSONArray := TJSONArray.Create; ja.Add( '中国' ); ja.Add( '程序员' ); jo.AddPair( 'other' , ja); Memo2.Lines.Add(jo.ToString); //{"name":"张三","other":["中国","程序员"]} Memo2.Lines.Add(jo.ToJSON); //{"name":"\u5F20\u4E09","other":["\u4E2D\u56FD","\u7A0B\u5E8F\u5458"]} jo.Free; end; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | const s = '[{"id":1,"name":"咏南"},{"id":2,"name":"中间件"}]' ; procedure newFields( const ds: TDataSet; const ja: TJSONArray; const strFieldSize: Integer = 256); begin var jo: TJSONObject := ja.Items[0] as TJSONObject; ds.Close; ds.FieldDefs.Clear; for var pair: TJSONPair in jo do begin var fieldName: string := pair.JsonString.Value; var fieldSize: Integer := 0; if pair.JsonValue is TJSONString then begin fieldSize := strFieldSize; ds.FieldDefs.Add(fieldName, ftString, fieldSize) end else if pair.JsonValue is TJSONNumber then begin ds.FieldDefs.Add(fieldName, ftFloat); end; end; if ds is TClientDataSet then TClientDataSet(ds).CreateDataSet; end; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | procedure TForm2.Button1Click(Sender: TObject); begin var s: string := '{"name":"咏南中间件","age":12}' ; var jo: TJSONObject := TJSONObject.Create; jo.AddPair( 'f1' , s); Memo1.Lines.add(jo.ToJSON); //{"f1":"{\"name\":\"\u548F\u5357\u4E2D\u95F4\u4EF6\",\"age\":12}"} var jo2: TJSONObject := TJSONObject.ParseJSONValue(jo.ToString) as TJSONObject; s := jo2.tostring; //{"f1":"{\"name\":\"咏南中间件\",\"age\":12}"} s := StringReplace(s, '\"' , '"' , [rfReplaceAll]); memo1.Lines.add(s); //{"f1":"{"name":"咏南中间件","age":12}"} jo.free; jo2.free; end; |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/12605909.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2019-03-31 FDMEMTABLE将修改后的数据序列为JSON
2019-03-31 firedac数据集数据序列为JSON
2017-03-31 ubuntu允许mysql远程连接
2016-03-31 咏南中间件集群