本文整理自网络,侵删。
procedure TFmMain.BtnIPQueryClick(Sender: TObject);
var
S: string;
JSONStr: string;
country, area, region, city: string;
begin
//EditIP 是输入的要查询的 IP 地址
S := 'http://ip.taobao.com/service/getIpInfo.php?ip=';
JSONStr := IdHTTP1.Get(S + EditIP.Text);
if JSONStr = '' then Exit;
ParseIPAddressFromTaoBaoJSON(JSONStr, country, area, region, city);
Memo1.Lines.Add(country + '; ' + area + '; ' + region + '; ' + City);
end;
---------------------
这个函数使用了 TJSONValue 需要 uses System.JSON;
代码如下:
function TFmMain.ParseIPAddressFromTaoBaoJSON(const JSONStr: string; var country,
area, region, city: string): Boolean;
var
Obj: TJSONValue;
AValue: TJSONValue;
begin
Obj := TJSONObject.ParseJSONValue(JSONStr);
if Obj is TJSONObject then
begin
AValue := TJSONObject(Obj).Values['data'];
end;
if AValue is TJSONObject then
begin
country := TJSONObject(AValue).Values['country'].ToString;
area := TJSONObject(AValue).Values['area'].ToString;
region := TJSONObject(AValue).Values['region'].ToString;
city := TJSONObject(AValue).Values['city'].ToString;
end;
end;
---------------------
作者:pcplayer
来源:CSDN
原文:https://blog.csdn.net/pcplayer/article/details/80793709
相关阅读 >>
检查是否在Delphi xe7中启用或禁用了android蓝牙
更多相关阅读请进入《Delphi》频道 >>