Delphi Web服务JSON数组
我对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚找到了正确的答案。代码如下:
请求为
http://localhost/sample1?json={"names":["john","jay"]}
输出是
希望这可以帮助刚使用 delphi 创建 Web 服务请求的人。
i have just found the right answer. Here's the code:
The request is
http://localhost/sample1?json={"names":["john","jay"]}
The output is
Hope this can help someone who is new in creating web service request using delphi.
首先,我认为你的问题中显示的 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.