DELPHI 代码查询一个json IP 地址的归属地


本文整理自网络,侵删。

 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研究之驱动开发篇(五)--使用后备列表

Delphi isdirectory 判断是否是目录

dll 的静态调用实例代码

Delphi研究之驱动开发篇(二)--工具及环境搭建

Delphi 提升权限查找进程关闭进程单元

Delphi fastreport 直接列印

Delphi中限制鼠标的移动区域

Delphi显示gif动画简单方法

检查是否在Delphi xe7中启用或禁用了android蓝牙

Delphi安卓创建pdf文件并打开

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...