delphi 代码查询一个json IP 地址的归属地  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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 


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.041485071182251 seconds