json - Delphi:将 JSON 数组添加到 JSON 对象

标签 json delphi delphi-2010

我正在使用 Indy 调用第 3 方 API

var loRespJson: TMemoryStream;
IdHTTP1.GET(lsURL, loRespJson)

它返回一个 JSON 数组:

[
    {
        "Active": "1",
        "SourceId": "215",
        "SourceName": "MyVal1"
    },
    {
        "Active": "1",
        "SourceId": "383",
        "SourceName": "MyVal2"
    }
]

反过来,我的函数创建一个新的 JSON 对象,添加附加信息和响应,并将其返回给调用程序。期望的结果:

{
    "responseCode":"200",
    "companyNo":"0268895",
    "responseMessage": [
        {
            "Active": "1",
            "SourceId": "215",
            "SourceName": "MyVal1"
        },
        {
            "Active": "1",
            "SourceId": "383",
            "SourceName": "MyVal2"
        }
    ]
}

如何才能实现上述目标?如果我使用以下内容添加,它会在数组周围创建“”(引号),这在解析 JSON 时是一个大问题:

loJSon.AddPair(TJSONPair.Create('responseCode', IntToStr(idHttp1.ResponseCode)));
loJSon.AddPair(TJSONPair.Create('companyNo', CompanyNo));

if idHttp1.ResponseCode = 200 then
begin
  lsRespMsg := StreamToString(loRespJSon);
  liSuper := SO(lsRespMsg);
  loJSon.AddPair(TJSONPair.Create('responseMessage', liSuper.AsJSon()));
  …

我也尝试过循环遍历 JSON 数组,但该选项在每个数组项周围添加“”

{ create an json-array }
loJSA := TJsonArray.Create();
{ add array to object }
loJSP := TJSONPair.Create('responseMessage', loJSA);
loJSon.AddPair(loJSP);

if liSuper.IsType(stArray) then
begin
  for i := 0 to liSuper.AsArray.Length - 1 do
  begin
    loSubscription := liSuper.AsArray[i];
    loJSA.Add(loSubscription.AsJSon());
  end;
end;

任何帮助将不胜感激!谢谢。

最佳答案

您似乎使用了单元 System.JSON 中的 loJSon: TJSONObject在您尝试构建 JSON 新响应时。然后,您使用 SuperObject 中的函数 SO() 解析从 TIdHTTP 收到的响应正文。库,与 System.JSON 不太兼容。

到目前为止,该方法很好,但您应该坚持使用单个库。解析响应后,您添加了一对 TJSONObject,其值为 liSuper.AsJSon() (一个字符串)。这就解释了用双引号括住数组的原因。

仅使用 System.JSON 的正确解决方案可能如下所示:

uses
  System.SysUtils, System.JSON;

const
  ResponseMessageStr = '[{"Active":"1","SourceId":"215","SourceName":"MyVal1"},{"Active":"1","SourceId":"383","SourceName":"MyVal2"}]';
var
  ResponseJSON: TJSONObject;
begin
  ResponseJSON := TJSONObject.Create;
  try
    ResponseJSON.AddPair('responseCode', '200');
    ResponseJSON.AddPair('companyNo', '0268895');
    ResponseJSON.AddPair('responseMessage', TJSONObject.ParseJSONValue(ResponseMessageStr));
    Writeln(ResponseJSON.Format());
  finally
    ResponseJSON.Free;
  end;
end.

在上面的代码片段中我使用了 TJSONObject.ParseJSONValue解析响应消息并将其附加到生成的 JSON 对象。尽管我的响应消息存储在字符串常量中,但您可以轻松调整该解决方案以与 TMemoryStreamTIdHTTP 一起使用。查看 TJSONObject.ParseJSONValue 的所有不变量。

代码片段产生输出:

{
    "responseCode": "200",
    "companyNo": "0268895",
    "responseMessage": [
        {
            "Active": "1",
            "SourceId": "215",
            "SourceName": "MyVal1"
        },
        {
            "Active": "1",
            "SourceId": "383",
            "SourceName": "MyVal2"
        }
    ]
}

另请注意,为了方便起见,TJSONObject.AddPair 有多个不变量。另一点是,我使用 Format 方法来漂亮地打印 JSON 对象的内容,但您可能应该在生产中使用 ToJSON

使用 SuperObject 库产生等效结果的解决方案:

uses
  System.SysUtils, SuperObject;

const
  ResponseMessageStr = '[{"Active":"1","SourceId":"215","SourceName":"MyVal1"},{"Active":"1","SourceId":"383","SourceName":"MyVal2"}]';
var
  ResponseJSON: ISuperObject;
begin
  ResponseJSON := TSuperObject.Create;
  ResponseJSON.I['responseCode'] := 200;
  ResponseJSON.S['responseCode'] := '0268895';
  ResponseJSON.O['responseMessage'] := SO(ResponseMessageStr);
  Writeln(ResponseJSON.AsJSon(True));
  Readln;
end.

关于json - Delphi:将 JSON 数组添加到 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63561721/

相关文章:

delphi - WSDL 导入器生成有故障的服务器

sql-server - 使用 Entity Framework 发布 Web API 时出错

mysql - 将json字符串插入mysql

jquery - 使用 GET 从 jqGrid 中的列值链接到新页面

delphi - 如何从 Delphi 迁移到 Delphi 2010(Unicode 问题)

delphi - 在Delphi 2010中绘制不透明度椭圆

jquery - 在 jquery datatables.net 中呈现 bool 数据列的最佳方法

delphi - 在运行时将对象强制转换为其父对象

Delphi 2010 控制闪烁

delphi - Delphi-如何监控网络