gpt4 book ai didi

json - 用 Delphi 构建 JSON

转载 作者:行者123 更新时间:2023-12-03 18:10:00 28 4
gpt4 key购买 nike

我正在尝试实现一个函数,该函数返回一个包含类元素的 json 对象。这是我的功能:

procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonPair: TJSONPair;
jsonObject: TJSONObject;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();
jsonObject := TJSONObject.Create();
jsonPair := TJSONPair.Create('ratings', jsonArray);

for i := 0 to AInputList.Count - 1 do
begin
jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));

jsonArray.AddElement(jsonObject);
end;

AResponse.AddPair(jsonPair);
end;

当我使用包含两个元素的列表对其进行测试时,返回的字符串是:
{
"ratings":[{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03",
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24"
},{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03",
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24"
}]
}

我试图在每次循环迭代后删除所有对:
procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonPair: TJSONPair;
jsonObject: TJSONObject;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();
jsonObject := TJSONObject.Create();
jsonPair := TJSONPair.Create('ratings', jsonArray);

for i := 0 to AInputList.Count - 1 do
begin
jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));

jsonArray.AddElement(jsonObject);
jsonObject.RemovePair('idrating');
jsonObject.RemovePair('idmark');
jsonObject.RemovePair('value');
jsonObject.RemovePair('description');
jsonObject.RemovePair('timeposted');
end;

AResponse.AddPair(jsonPair);
end;

输出是一个包含 n(count of list elements) 空对象的数组: {"ratings":[{},{}]}
我试图构建的 json 应该是这样的:
{
"ratings":[{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03"
},{
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24"
}]
}

最佳答案

您有一个 jsonObject,因此每次将其添加到数组时都会重复它最终处于的状态。

在循环中构造 jsonObject,然后你的数组中就会有不同的对象。

  for i := 0 to AInputList.Count - 1 do
begin
jsonObject := TJSONObject.Create();
jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));

jsonArray.AddElement(jsonObject);
end;

或者,您可以定义一个函数来从 TRating 创建一个 TJSONObject,并在您的循环中使用它
function TRatingToJSON( Rating: TRating ): TJSONObject;
begin
Result := TJSONObject.Create();

Result.AddPair(TJSONPair.Create('idrating', IntToStr(Rating.IdRating)));
Result.AddPair(TJSONPair.Create('idmark', IntToStr(Rating.IdMark)));
Result.AddPair(TJSONPair.Create('value', IntToStr(Rating.Value)));
Result.AddPair(TJSONPair.Create('description', Rating.Description));
Result.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', Rating.TimePosted)));
end;

procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();

for i := 0 to AInputList.Count - 1 do
begin
jsonArray.AddElement(TRatingToJSON(AInputList[i]));
end;

AResponse.AddPair(TJSONPair.Create('ratings', jsonArray));
end;

关于json - 用 Delphi 构建 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31698703/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com