如何使用 Delphi 解析 json 字符串响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31390545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse a json string response using Delphi
提问by Luiz Alves
I have a rest server returning the next json string:
我有一个休息服务器返回下一个 json 字符串:
response:='{"result":["[{\"email\":\"[email protected]\",\"regid\":\"12312312312312312313213w\"},{\"email\":\"[email protected]\",\"regid\":\"AAAAAAA\"}]"]}';
I′d like to parse the response to get a list of all emailand regiditems.
我想解析响应以获取所有的清单email和regid项目。
I have tried the next code but I am getting an AV at (TJSONPair(LItem).JsonString.Value='email')
我已经尝试了下一个代码,但我在 (TJSONPair(LItem).JsonString.Value='email')
Any help will be appreciated.
任何帮助将不胜感激。
Thanks in advance, Luiz
提前致谢,路易斯
var
LResult:TJSONArray;
LJsonresponse:TJSONObject;
i:integer;
LItem,jv:TJsonValue;
email,regid:string;
LJsonresponse:=TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(response),0) as TJSONObject;
LResult:=(LJsonresponse.GetValue('result') as TJSONArray);
jv:=TJSONArray(LResult.Get(0));
for LItem in TJSONArray(jv) do begin
if (TJSONPair(LItem).JsonString.Value='email') then begin
email:=TJSONPair(LItem).JsonValue.Value;
end;
if (TJSONPair(LItem).JsonString.Value='regid') then begin
regid:=TJSONPair(LItem).JsonValue.Value;
end;
end;
回答by David Heffernan
Your problems start here:
您的问题从这里开始:
jv := TJSONArray(LResult.Get(0));
The problem is that LResult.Get(0)does not return an instance of TJSONArray. In fact it returns an instance of TJSONString. That string has value:
问题是LResult.Get(0)不返回TJSONArray. 事实上,它返回 的一个实例TJSONString。该字符串具有价值:
'[{"email":"[email protected]","regid":"12312312312312312313213w"},{"email":"[email protected]","regid":"AAAAAAA"}]'
It looks like you are going to need to parse this string as JSON to extract what you need. Here is some gnarly code that does that. Please excuse its quality because I have no experience at all with the Delphi JSON parser.
看起来您需要将此字符串解析为 JSON 以提取您需要的内容。这里有一些粗糙的代码可以做到这一点。请原谅它的质量,因为我对 Delphi JSON 解析器完全没有经验。
{$APPTYPE CONSOLE}
uses
SysUtils, JSON;
const
response =
'{"result":["[{\"email\":\"[email protected]\",\"regid\":\"12312312312312312313213w\"},'+
'{\"email\":\"[email protected]\",\"regid\":\"AAAAAAA\"}]"]}';
procedure Main;
var
LResult: TJSONArray;
LJsonResponse: TJSONObject;
ja: TJSONArray;
jv: TJSONValue;
begin
LJsonResponse := TJSONObject.ParseJSONValue(response) as TJSONObject;
LResult := LJsonResponse.GetValue('result') as TJSONArray;
ja := TJSONObject.ParseJSONValue(LResult.Items[0].Value) as TJSONArray;
for jv in ja do begin
Writeln(jv.GetValue<string>('email'));
Writeln(jv.GetValue<string>('regid'));
end;
end;
begin
try
Main;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
The big lesson here is to stop using unchecked type casts. Using such casts is asking for trouble. When your data does not match your code, you get unhelpful error messages.
这里的重要教训是停止使用未经检查的类型转换。使用这种类型转换是自找麻烦。当您的数据与您的代码不匹配时,您会收到无用的错误消息。

