简体   繁体   English

如何发送/接收JSON?

[英]How to send/receive JSON?

A dumb question, I know. 一个愚蠢的问题,我知道。 But I don't normally do web programming and have been asked to send some JSON data and receive some in return. 但我通常不会进行Web编程,并且被要求发送一些JSON数据并收到一些作为回报。

I googled and it is clear how to encode and decode the JSON data, but I am so new to this that I don't know how to communicate with the given URL and say "here's some data, please send me some back". 我用谷歌搜索,很清楚如何编码和解码JSON数据,但我对此很新,我不知道如何与给定的URL进行通信,并说“这里有一些数据,请给我一些回复”。

Can anyone point me at a simple working code example? 有人能指出一个简单的工作代码示例吗? For instance, just to pass user_name and password as JSON and to receive some reply ... I think I should be using TIdHTTP , but am not sure how to use it with JSON. 例如,只是将user_name和密码作为JSON传递并接收一些回复......我想我应该使用TIdHTTP ,但我不确定如何将它与JSON一起使用。 I have one GET and one POST interface. 我有一个GET和一个POST界面。

+1 to @DavidHeffernan but I don't understand who to pass the packed JSON data with my request or to retrieve it from the response. +1到@DavidHeffernan但我不明白谁将我的请求传递打包的JSON数据或从响应中检索它。 En/de-code is fine, but adding it as parameters has me bogged down. En / de-code很好,但添加它作为参数让我陷入困境。

I really would appreciate a (link to a) small, but complete working example. 我真的很感激(链接到)一个小而完整的工作示例。

Btw, I only have XE2 starter edition, so I don't have datasnap. 顺便说一句,我只有XE2入门版,所以我没有datasnap。

For the JSON process via HTTP, take a look at the following S/O question: https://stackoverflow.com/a/15948106/458259 对于通过HTTP的JSON进程,请查看以下S / O问题: https//stackoverflow.com/a/15948106/458259

If your problem is about how to consume a remote RESTful service in Delphi, you will find it the answer. 如果您的问题是如何在Delphi中使用远程RESTful服务,您将找到答案。

But if you want to communicate between Delphi applications using JSON and HTTP, the easiest is to implemenent a RESTful Client-Server connection in Delphi. 但是如果你想使用JSON和HTTP在Delphi应用程序之间进行通信,最简单的方法就是在Delphi中实现RESTful Client-Server连接。

For Client-Server transmission using JSON and RESTful, take a look at our Open Source Synopse mORMot framework . 对于使用JSON和RESTful的客户端 - 服务器传输,请查看我们的开源Synopse mORMot框架 It will do all the JSON serialization and RESTful routing for you. 它将为您执行所有JSON序列化和RESTful路由。

It features a Client-Server ORM (perhaps not what you need here). 它具有客户端 - 服务器ORM(可能不是您需要的)。

But also a SOA framework using two modes: 但也是一个使用两种模式的SOA框架:

You can chose either mode to use JSON communication over HTTP. 您可以选择任一模式以通过HTTP使用JSON通信。

Note that mORMot is expected to work with Delphi XE2 starter edition , since it does not use any "advanced" feature of the Pro/Entreprise/Architect editions, like DB.pas or DataSnap. 请注意, mORMot预计可与Delphi XE2入门版配合使用 ,因为它不使用Pro / Entreprise / Architect版本的任何“高级”功能,如DB.pas或DataSnap。 And it will allow you to access databases, even with a starter edition. 它允许您访问数据库,即使使用入门版本也是如此。 In case of difficulties due to something missing in the Starter, please feel free to report the problem on our forum. 如果由于Starter缺少某些东西而遇到困难,请随时在我们的论坛上报告问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM