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 tbitmap创建时提示object or class type required

Delphixe jpg图片压缩

Delphi的ttreeview类使用大全

Delphi dbgrid支持鼠标滚轮浏览数据

Delphi rect()

Delphi 的链式代码

Delphi adoquery查询,如何得到查询记录数?

Delphi 双击listbox1内容,数据插入到memo1/synedit1鼠标指定位置

Delphi 用浏览器来显示带图片的邮件内容,图片无需保存为本地文件

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



打赏

取消

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

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

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

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

评论

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