如何反序列化 IPGeoLocation API 返回的 json 并在 .chtml 视图中使用
Posted
技术标签:
【中文标题】如何反序列化 IPGeoLocation API 返回的 json 并在 .chtml 视图中使用【英文标题】:How to deserialize json returned by IPGeoLocation API and Use it in .chtml View 【发布时间】:2020-01-04 05:35:26 【问题描述】:我正在尝试编写一个代码来获取特定 IP 的数据,我使用了这个名为 Free IP GeoLocation API 的 API
https://rapidapi.com/jkosgei/api/free-ip-geolocation
我已尝试列出一些数据并尝试对其进行反序列化,但它没有返回任何内容。
"\n \"ip\": \"72.255.29.148\",\n \"is_eu\": false,\n \"city\": \"Karachi\",\n \"region\": \"Sindh\",\n \"region_code\": \"SD\",\n \"country_name\": \"Pakistan\",\n \"country_code\": \"PK\",\n \"continent_name\": \"Asia\",\n \"continent_code\": \"AS\",\n \"latitude\": 24.9043,\n \"longitude\": 67.0817,\n \"asn\": \"AS9541\",\n \"organisation\": \"Cyber Internet Services (Pvt) Ltd.\",\n \"postal\": \"12311\",\n \"calling_code\": \"92\",\n \"flag\": \"https://ipdata.co/flags/pk.png\",\n \"emoji_flag\": \"\\ud83c\\uddf5\\ud83c\\uddf0\",\n \"emoji_unicode\": \"U+1F1F5 U+1F1F0\",\n \"languages\": [\n \n \"name\": \"English\",\n \"native\": \"English\"\n ,\n \n \"name\": \"Urdu\",\n \"native\": \"\\u0627\\u0631\\u062f\\u0648\",\n \"rtl\": 1\n \n ],\n \"currency\": \n \"name\": \"Pakistani Rupee\",\n \"code\": \"PKR\",\n \"symbol\": \"PKRs\",\n \"native\": \"\\u20a8\",\n \"plural\": \"Pakistani rupees\"\n ,\n \"time_zone\": \n \"name\": \"Asia/Karachi\",\n \"abbr\": \"PKT\",\n \"offset\": \"+0500\",\n \"is_dst\": false,\n \"current_time\": \"2019-08-31T11:03:28.948199+05:00\"\n ,\n \"threat\": \n \"is_tor\": false,\n \"is_proxy\": false,\n \"is_anonymous\": false,\n \"is_known_attacker\": false,\n \"is_known_abuser\": false,\n \"is_threat\": false,\n \"is_bogon\": false\n ,\n \"count\": \"11\"\n"
我得到了反序列化的空值,这是我的 api 调用和所有控制器代码
var client = new RestClient("https://jkosgei-free-ip-geolocation-v1.p.rapidapi.com/72.255.29.148?api-key=66b8727d81306cd56a5300b77ee8d4699c85d8a87ff91d42cb62f10e");
var request = new RestRequest(Method.GET);
request.AddHeader("x-rapidapi-host", "jkosgei-free-ip-geolocation-v1.p.rapidapi.com");
request.AddHeader("x-rapidapi-key", "0fd0540211msh6eb28a39b114e5ap116f3fjsn60c6c9371c26");
IRestResponse response = client.Execute(request);
var CachedData = response.Content;
string json = CachedData.ToString();
var deserialized = JsonConvert.DeserializeObject<IPList>(json);
return View(deserialized.IPData);
这是我用过的两个模型
public class IPList
public IEnumerable<IP> IPData get; set;
public class IP
public int ip get; set;
public int is_eu get; set;
public int city get; set;
public int region get; set;
public int region_code get; set;
public int country_name get; set;
public int country_code get; set;
public int continent_name get; set;
public int continent_code get; set;
public int latitude get; set;
public int longitude get; set;
public int asn get; set;
public int organisation get; set;
public int postal get; set;
public int calling_code get; set;
public int flag get; set;
public int emoji_flag get; set;
public int emoji_unicode get; set;
public int currency get; set;
当我运行程序时,它在变量 deserialized 和 deserialized.IPData 中返回 null
我是反序列化的新手,所以请不要评判我
【问题讨论】:
【参考方案1】:问题在于 IP
类的结构不正确。
您提供的 json 的正确结构是这样的:
public class Language
public string name get; set;
public string native get; set;
public int? rtl get; set;
public class Currency
public string name get; set;
public string code get; set;
public string symbol get; set;
public string native get; set;
public string plural get; set;
public class TimeZone
public string name get; set;
public string abbr get; set;
public string offset get; set;
public bool is_dst get; set;
public DateTime current_time get; set;
public class Threat
public bool is_tor get; set;
public bool is_proxy get; set;
public bool is_anonymous get; set;
public bool is_known_attacker get; set;
public bool is_known_abuser get; set;
public bool is_threat get; set;
public bool is_bogon get; set;
public class IP
public string ip get; set;
public bool is_eu get; set;
public string city get; set;
public string region get; set;
public string region_code get; set;
public string country_name get; set;
public string country_code get; set;
public string continent_name get; set;
public string continent_code get; set;
public double latitude get; set;
public double longitude get; set;
public string asn get; set;
public string organisation get; set;
public string postal get; set;
public string calling_code get; set;
public string flag get; set;
public string emoji_flag get; set;
public string emoji_unicode get; set;
public List<Language> languages get; set;
public Currency currency get; set;
public TimeZone time_zone get; set;
public Threat threat get; set;
public string count get; set;
此外,我想强调一下,如果尝试反序列化您在上面提供的 JSON,因为 IPList
应该不起作用。
如果您想反序列化该 JSON,您可能需要使用它:
var deserialized = JsonConvert.DeserializeObject<IP>(json);
【讨论】:
以上是关于如何反序列化 IPGeoLocation API 返回的 json 并在 .chtml 视图中使用的主要内容,如果未能解决你的问题,请参考以下文章
Ktor:如何序列化/反序列化 JSON-API (vnd.api+json)
如何手动反序列化 JSON 并像 Web API 那样自动验证模型?