如何反序列化 IPGeoLocation API 返回的 json 并在 .chtml 视图中使用它
How to deserialize json returned by IPGeoLocation API and Use it in .chtml View
我正在尝试编写一个代码来获取特定 IP 的数据,我使用了这个 API 名为 Free IP GeoLocation API
https://rapidapi.com/jkosgei/api/free-ip-geolocation
我已经尝试列出一些数据并尝试对其进行反序列化,但它 returns 什么都没有。
"{\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; }
}
当我 运行 程序时它 returns 反序列化变量中的 null 并且 deserialized.IPData
我是反序列化的新手所以请不要评判我
问题是 IP
class 没有正确的结构。
您提供的 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);
我正在尝试编写一个代码来获取特定 IP 的数据,我使用了这个 API 名为 Free IP GeoLocation API
https://rapidapi.com/jkosgei/api/free-ip-geolocation
我已经尝试列出一些数据并尝试对其进行反序列化,但它 returns 什么都没有。
"{\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; }
}
当我 运行 程序时它 returns 反序列化变量中的 null 并且 deserialized.IPData
我是反序列化的新手所以请不要评判我
问题是 IP
class 没有正确的结构。
您提供的 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);