Delphi 自带的JSON内存释放问题
使用了 小飞鱼的例子( http://blog.csdn.net/gzxiaorou/article/details/44538193)解释JSON,在没有释放内存的情况不出现问题, 但在解释完释放变量,即出现了错误的指针。
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.json;
type
TForm2 = class(TForm)
mmo1: TMemo;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
JSONObject: TJSONObject;
LItem: TJSONValue;
LJPair: TJSONPair;
weather: TJSONArray;
StrJson: String;
result: String;
i: Integer;
begin
StrJson := mmo1.text;//假定是上面那个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;
Memo1.Text:= result;
weather.Free;
JSONObject.Free; // 加上这个, 会出现内存错误, 请专家帮忙解释一下.
end;
end.
只有json 数据带有数组的情况会出错,没有数组的不出错 。 这个是system.json的一个BUG吗?
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.json;
type
TForm2 = class(TForm)
mmo1: TMemo;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
JSONObject: TJSONObject;
LItem: TJSONValue;
LJPair: TJSONPair;
weather: TJSONArray;
StrJson: String;
result: String;
i: Integer;
begin
StrJson := mmo1.text;//假定是上面那个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;
Memo1.Text:= result;
weather.Free;
JSONObject.Free; // 加上这个, 会出现内存错误, 请专家帮忙解释一下.
end;
end.
只有json 数据带有数组的情况会出错,没有数组的不出错 。 这个是system.json的一个BUG吗?
...全文
相关推荐
Delphi 7下的JSON解析
标题提及的"Delphi 7下的JSON解析"是指在Delphi 7这个版本中处理JSON数据的方法。
delphi 使用json 示例
这个“delphi 使用json 示例”提供了很好的实践基础,帮助开发者更好地理解和应用JSON与Delphi的结合。记住,始终确保正确释放分配的内存,特别是在处理动态创建的JSON对象时。
Delphi_JSON delphi7解析JSON控件
兼容性:既然能在“新测”中使用,说明控件可能已经解决了与最新Delphi版本不兼容的问题,或者至少在一定程度上支持了新的编译器特性。
通过Json数据格式进行Http服务交互(Delphi)
本篇文章将详细探讨如何使用Delphi通过Json数据格式进行Http服务的交互,主要涉及的知识点包括Json、Http协议以及Delphi中的IdHTTP组件。
delphi数据集和json互转
清理资源:解析完成后,记得释放lkJSON的对象。```delphi JSONObj.Free;```以上就是使用lkJSON-1.07库在Delphi中实现数据集与JSON互转的基本方法。


网络通信/分布式开发
