Delphi Web服务JSON数组

发布于 2024-11-16 12:19:30 字数 1778 浏览 6 评论 0原文

我对Delphi真的很陌生,我正在做一个关于如何通过delphi输出JSON数组的实验。这对任何人来说可能听起来都很简单,但我只是不知道如何做。我已经创建了一个简单的程序。

现在,我想做的是创建一个带有如下参数的命令/请求:

http://localhost:8001/hello?json={"names":["Jay","Chris","John"]}

这将在浏览器中创建如下结果:

{ 结果:[“你好杰伊”,“你好克里斯”,“你好约翰”], ID: ””, 已用时间:0 }

拜托,我真的需要这方面的帮助。有人吗?

编辑: 这是我今天刚做的代码,但它仍然没有显示我想要的输出:

procedure TPrimeJSONMHelloPeople.ProcessJSONRPCRequest(
  var ResultValue: TlkJSONbase; var ResultSuccess: Boolean);
  var

    jsonPeople:TlkJSONlist;
    dmPool:TObject;
    dm:TPrimeDataModuleBaseDM;
    i:integer;

  begin
    FjsonObj1 := TlkJSONobject.Create;
    jsonPeople := FjsonObj1.CreateListValue('names');
    jsonPeople.AddVarString('jay');
    jsonPeople.AddVarString('ann');
    jsonPeople.AddVarString('john');
    inherited;

    CheckRequiredParameter('names');

    PrimeDataModuleWebService.TDataModuleDMCreateInstanceDefault(dmPool);
      try
         dm := TPrimeDataModuleDefaultDM(dmPool).GetModule;
         try

         //this part here will loop and output the name
         //if jsonPeople <> nil then

         if Params.Field['names'] <> nil then
           begin
             for i := 0 to FjsonObj1.Field['names'].Count - 1 do
             begin           
               ResultValue := TlkJSONlist.Create
             end;

         end;
       ResultValue := TlkJSONlist.Create;
       finally
       dm.Release;
      end;
    finally
  dmPool.Free;
 end;
   FjsonObj1.Free;
   ResultSuccess := True;
 end;

我不知道代码中缺少什么,它只显示: {

result: [ ],
id: "",
time_elapsed: 0

}

而不是: <代码>{ 结果:[“你好杰伊”,“你好克里斯”,“你好约翰”], ID: ””, 已用时间:0 }

I am really new to Delphi and I am doing an experiment on how to output JSON array through delphi. This maybe sound simple to anyone but I just dont know how. I already created a simple program.

Now, what i want to do is to create a command/request with parameter like:

http://localhost:8001/hello?json={"names":["Jay","Chris","John"]}

that would create a result in the browser like this:

{
result: ["Hello Jay","Hello Chris","Hello John"],
id: "",
time_elapsed: 0
}

Please, i really need help on this. Anybody?

EDIT:
This is the code i just did today but it still doesn't show my desired output:

procedure TPrimeJSONMHelloPeople.ProcessJSONRPCRequest(
  var ResultValue: TlkJSONbase; var ResultSuccess: Boolean);
  var

    jsonPeople:TlkJSONlist;
    dmPool:TObject;
    dm:TPrimeDataModuleBaseDM;
    i:integer;

  begin
    FjsonObj1 := TlkJSONobject.Create;
    jsonPeople := FjsonObj1.CreateListValue('names');
    jsonPeople.AddVarString('jay');
    jsonPeople.AddVarString('ann');
    jsonPeople.AddVarString('john');
    inherited;

    CheckRequiredParameter('names');

    PrimeDataModuleWebService.TDataModuleDMCreateInstanceDefault(dmPool);
      try
         dm := TPrimeDataModuleDefaultDM(dmPool).GetModule;
         try

         //this part here will loop and output the name
         //if jsonPeople <> nil then

         if Params.Field['names'] <> nil then
           begin
             for i := 0 to FjsonObj1.Field['names'].Count - 1 do
             begin           
               ResultValue := TlkJSONlist.Create
             end;

         end;
       ResultValue := TlkJSONlist.Create;
       finally
       dm.Release;
      end;
    finally
  dmPool.Free;
 end;
   FjsonObj1.Free;
   ResultSuccess := True;
 end;

I don't know what's missing in the code, It only shows:
{

result: [ ],
id: "",
time_elapsed: 0

}

and not :
{
result: ["Hello Jay","Hello Chris","Hello John"],
id: "",
time_elapsed: 0
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ζ澈沫 2024-11-23 12:19:30

我刚刚找到了正确的答案。代码如下:

procedure TSample1.ProcessJSONRPCRequest(
  var ResultValue: TlkJSONbase; var ResultSuccess: Boolean);
  var

    dmPool:TObject;
    dm:TPrimeDataModuleBaseDM;

    jsonPeople:TlkJSONlist;    //used Tlkjsonlist since I want to create an array
    i:integer;
  begin
  inherited;
    jsonPeople:=TlkJSONlist.Create;  //create jsonPeople as an array

    CheckRequiredParameter('names'); //names parameter needed
    PrimeDataModuleWebService.TDataModuleDMCreateInstanceDefault(dmPool);
    try
      dm := TPrimeDataModuleDefaultDM(dmPool).GetModule;
      try
        if Params.Field['names'] <> nil then //check if the names parameter is empty
        begin
          ResultValue:=jsonPeople;
          for i := 0 to Params.Field['names'].Count - 1 do
          begin
            jsonPeople.AddVarString('hello ' + Params.Field['names'].Child[i].value);
          end;
        end;
      finally
        dm.Release;
      end;
    finally
    dmPool.Free;
  end;
  ResultSuccess := True;
end;

end.

请求为 http://localhost/sample1?json={"names":["john","jay"]}
输出是

{

    -
    result: [
        "hello john"
        "hello jay"
    ]
    id: ""
    time_elapsed: 0

}

希望这可以帮助刚使用 delphi 创建 Web 服务请求的人。

i have just found the right answer. Here's the code:

procedure TSample1.ProcessJSONRPCRequest(
  var ResultValue: TlkJSONbase; var ResultSuccess: Boolean);
  var

    dmPool:TObject;
    dm:TPrimeDataModuleBaseDM;

    jsonPeople:TlkJSONlist;    //used Tlkjsonlist since I want to create an array
    i:integer;
  begin
  inherited;
    jsonPeople:=TlkJSONlist.Create;  //create jsonPeople as an array

    CheckRequiredParameter('names'); //names parameter needed
    PrimeDataModuleWebService.TDataModuleDMCreateInstanceDefault(dmPool);
    try
      dm := TPrimeDataModuleDefaultDM(dmPool).GetModule;
      try
        if Params.Field['names'] <> nil then //check if the names parameter is empty
        begin
          ResultValue:=jsonPeople;
          for i := 0 to Params.Field['names'].Count - 1 do
          begin
            jsonPeople.AddVarString('hello ' + Params.Field['names'].Child[i].value);
          end;
        end;
      finally
        dm.Release;
      end;
    finally
    dmPool.Free;
  end;
  ResultSuccess := True;
end;

end.

The request is http://localhost/sample1?json={"names":["john","jay"]}
The output is

{

    -
    result: [
        "hello john"
        "hello jay"
    ]
    id: ""
    time_elapsed: 0

}

Hope this can help someone who is new in creating web service request using delphi.

苏别ゝ 2024-11-23 12:19:30

首先,我认为你的问题中显示的 URI 已经被解码。您应该在 HTTP 协议中对 URI 参数进行编码。

如果您想创建此类面向 HTTP 的 JSON 访问,请查看 RESTful 方法。它将帮助您不必重新发明井,并且能够使您的服务器更好地支持 AJAX。

那么你似乎使用了 第三方 lkJSON Delphi 库 ...所以你可以直接获得帮助它的作者或支持论坛。

从库的源代码中,您应该使用 TlkJSONlist 实例来处理来自 URI 输入和结果输出的 JSON 数组。

First of all, I think your URI shown in your question is already decoded. You should encode the URI parameters in the HTTP protocol.

If you want to create such HTTP-oriented JSON access, take a look at the RESTful approach. It would help you not reinvent the well, and be able to make your server more AJAX ready.

Then you seems to use the third-party lkJSON Delphi library... So you could get directly help from its author or support forum.

From the source code of the library, you should use a TlkJSONlist instance to handle a JSON array, from both URI input and result output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文