本文整理自网络,侵删。
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 tbitmap创建时提示object or class type required
Delphi 双击listbox1内容,数据插入到memo1/synedit1鼠标指定位置
Delphi 用浏览器来显示带图片的邮件内容,图片无需保存为本地文件
更多相关阅读请进入《Delphi》频道 >>