您好我使用API shodan使用uLkJSON在json中查看结果,问题是当我加载IP程序时,我总是返回错误"无效的类型转换"并停在那里。
代码是这样的:
unit tool;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, Vcl.StdCtrls, IdSSLOpenSSL,
IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, uLkJSON;
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
nave: TIdHTTP;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
GroupBox1: TGroupBox;
ip: TEdit;
Button1: TButton;
GroupBox2: TGroupBox;
console1: TMemo;
ssl: TIdSSLIOHandlerSocketOpenSSL;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
api_key: string;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
api_key := 'apikey';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
code: string;
var
js,
itjs: TlkJSONobject;
var ip_found:string;
begin
code := nave.Get('https://api.shodan.io/shodan/host/' + ip.Text + '?key='
+ api_key);
js := TlkJSON.ParseText(code) as TlkJsonObject;
ip_found := js.getString('ip');
console1.Lines.Add(ip_found);
end;
end.
错误是"无效的类类型转换"并出现在这一行:
ip_found := js.getString('ip');
使用按钮时会出现错误,而不是在我加载程序时发生错误,但是调试器会说"异常通知" "项目shodan.exe EInvalidCast引发了带有消息&#34的异常类;无效的类类型转换"
并将此行调试文件标记为uLkJSON
js := FieldByIndex[idx] as TlkJSONstring;
JSON代码:
有人可以帮忙吗?
答案 0 :(得分:2)
如果
ip_found := js.getString('ip');
导致无效的演员
js := FieldByIndex[idx] as TlkJSONstring;
然后明显的结论是名为ip
的值不是字符串。
如果没有能够看到JSON,那就很难说了。
<强>更新强>
您现在已经展示了一些JSON。这是关键部分:
"ip": 92779150
根据JSON语法,92779150
是一个数字而不是一个字符串,这就是getString
失败的原因。您可以使用getDouble
来读取值。