解析 JSON 字符串以检测错误响应

发布于 2024-10-06 10:21:42 字数 616 浏览 0 评论 0原文

我正在使用一个使用 JSON 格式响应的服务器。

当请求包含有效数据时,它们会使用如下字符串进行响应

{"数据":{"结果":[{"分支":"ACCT590006"}]}}

但如果请求的参数不正确,响应将如下所示

{"error":{"errors":[{"domain":"global","re​​ason":"无效","message":"无效 参数"}],"代码":98865,"消息":"无效 参数值"}}

问题是我如何使用 TJSONObject 对象并另外解析 JSON 字符串以显示消息和错误代码,如下所示。

Failed reason : invalid 
Message : Invalid params 
Code: 98865 
message : invalid param value.

i'm working with a server which response using the JSON format.

when the request contain valid data they respond with a string like this

{"data":{"results":[{"Branch":"ACCT590006"}]}}

but if the parameters of the request are incorrect the response goes like this

{"error":{"errors":[{"domain":"global","reason":"invalid","message":"Invalid
Params"}],"code":98865,"message":"Invalid
param value"}}

So the questions are how i can determine when the response of the server contains a error string using the TJSONObject object and additionally parse the JSON string to show the messages and error codes like this.

Failed reason : invalid 
Message : Invalid params 
Code: 98865 
message : invalid param value.

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

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

发布评论

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

评论(1

你对谁都笑 2024-10-13 10:21:42

每次我从代码(delphi 7)解析时,我都使用过一些 JSON。但我查了一下,在这里你可能会找到你问题的答案:
http://edn.embarcadero.com/print/40882

稍作调整,这应该可以工作。

谨致问候,
拉杜

I've worked a little with JSON, an every time I've parsed from code(delphi 7). But i've searched a little bit, and here you may find the answer of your question:
http://edn.embarcadero.com/print/40882

and with a little adaption this should work.

Best regards,
Radu

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