C# C处理来自ipdata.co的JSON
作为C语言的新手,我希望有人能帮助我理解我需要做什么 以下示例是ipdata.co文档网站上推荐的C代码C# C处理来自ipdata.co的JSON,c#,json,geoip,C#,Json,Geoip,作为C语言的新手,我希望有人能帮助我理解我需要做什么 以下示例是ipdata.co文档网站上推荐的C代码 using RestSharp; (at the top of the file) var client = new RestClient("https://api.ipdata.co/?api-key=test"); var request = new RestRequest(Method.GET); IRestResponse response = client.Execute(req
using RestSharp; (at the top of the file)
var client = new RestClient("https://api.ipdata.co/?api-key=test");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
我的api密钥有效。我可以手动查询并得到回复。现在,我试图解析来自上面示例代码中名为response的变量的JSON数据,这就是我丢失的地方
我正在从CSV文件中提取IP和域名
提交到ipdata.co并获取JSON文件作为回报
示例代码来自ipdata.co网站。除了它显示的内容,我不确定如何处理以JSON格式返回的数据,并提取我选择的特定元素,然后将结果写入磁盘
谷歌搜索让我更加困惑,所以我希望能在这里得到帮助
我有带有IP和域名的CSV文件。我将批量查询以获得lat/long和许多其他变量。我要解析并保存到磁盘的结果。这就是我迷路的地方,我希望有人不仅仅为我做代码,而且帮助我理解为什么我需要做别人建议的事情
下面是使用Google的8.8.8.8地址时返回的JSON文件
{
"ip": "8.8.8.8",
"is_eu": false,
"city": null,
"region": null,
"region_code": null,
"country_name": "United States",
"country_code": "US",
"continent_name": "North America",
"continent_code": "NA",
"latitude": 37.751,
"longitude": -97.822,
"postal": null,
"calling_code": "1",
"flag": "https://ipdata.co/flags/us.png",
"emoji_flag": "\ud83c\uddfa\ud83c\uddf8",
"emoji_unicode": "U+1F1FA U+1F1F8",
"asn": {
"asn": "AS15169",
"name": "Google LLC",
"domain": "google.com",
"route": "8.8.8.0/24",
"type": "hosting"
},
"languages": [
{
"name": "English",
"native": "English"
}
],
"currency": {
"name": "US Dollar",
"code": "USD",
"symbol": "$",
"native": "$",
"plural": "US dollars"
},
"time_zone": {
"name": "America/Chicago",
"abbr": "CST",
"offset": "-0600",
"is_dst": false,
"current_time": "2020-01-13T21:03:24.060857-06:00"
},
"threat": {
"is_tor": false,
"is_proxy": false,
"is_anonymous": false,
"is_known_attacker": false,
"is_known_abuser": true,
"is_threat": true,
"is_bogon": false
},
"count": "1"
}
如果出于某种原因,我不清楚我在问什么,只要告诉我我遗漏了什么,我会非常高兴地补充。我很喜欢学习C,但同时也有很多东西需要学习,我一直在学习我不知道的东西
提前感谢。要获取您要查找的数据,首先需要对response.Content进行反序列化 调用Rest端点时,可以用几种不同的方式调用它
var response = client.Execute(request); // 1
var response = client.Execute<YourClass>(request); // 2
现在您已经有了obj,您可以使用方括号访问任何数据来访问值。根据你的代码,它是这样的
Console.WriteLine(obj["ip"].ToString()); // Prints 8.8.8.8
Console.WriteLine(obj["time_zone"]["name"].ToString()); // Prints America/New_York
您可以类似地访问其他数据
其他方法
如果在进行API调用client.Executerequest时选择使用类,则不需要反序列化。您将把response.Data作为一个对象,您可以使用它从JSON访问属性
Console.WriteLine(response.Data.ip); // Prints 8.8.8.8
Console.WriteLine(response.Data.time_zone.name); // Prints America/New_York
希望这能澄清如何从API调用中获取数据
旁注
您可以使用在线站点,如json2csharp.com,将您的json转换为C类,您可以使用C类来反序列化您的响应。首先感谢您
我仍在尝试你发布的一些代码
这很管用,没有你的帮助我是做不到的
using System;
//Add
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using System.Globalization;
using IpData;
using RestSharp;
namespace ipdata_co_v1
{
class Program
{
public static void Main()
{
var client = new
RestClient("https://api.ipdata.co/8.8.8.8?api-key=test");
var request = new RestRequest(Method.GET);
//IRestResponse response = client.Execute(request);
//To get the data you are looking for, you will
first need to deserialize the response.Content.
//When you call the Rest endpoint, you can call it
in a few different ways.
//var response = client.Execute(request);
// 1
var response = client.Execute<IPData>(request); // 2
//var obj =
JsonConvert.DeserializeObject(response.Content);
//Console.WriteLine(obj["ip"].Value);
Console.WriteLine(response.Data.ip);
Console.WriteLine(response.Data.latitude);
Console.WriteLine(response.Data.longitude);
}
public class IPData
{
public string ip { get; set; }
public string city { 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 string latitude { get; set; }
public string longitude { get; set; }
public string flag { get; set; }
public string time_zone { get; set; }
}
}
}
它起作用了-
一旦我练习了更多,我想问更多的问题。首先需要几天的练习。因此,您可以使用API中的JSON和CSV。我假设您希望以CSV格式保存json中的数据。您希望保存JSON中的哪些信息?全部或部分?1>我从CSV文件中提取IP和域名2>提交到ipdata.co并在返回中获取JSON文件3>示例代码来自ipdata.co网站。除了它显示的内容之外,我通常还知道如何处理以JSON格式返回的数据,并提取我选择的特定元素,然后将结果写入磁盘。我希望这能澄清这一点。谢谢你,我一直在搜索谷歌,而我得到的回报让我更加困惑,这就是为什么我在这里问你。请注意,时区在你的课堂上不是一个字符串。它是另一个包含字符串的对象。使用json2csharp查看您需要的所有类
using System;
//Add
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using System.Globalization;
using IpData;
using RestSharp;
namespace ipdata_co_v1
{
class Program
{
public static void Main()
{
var client = new
RestClient("https://api.ipdata.co/8.8.8.8?api-key=test");
var request = new RestRequest(Method.GET);
//IRestResponse response = client.Execute(request);
//To get the data you are looking for, you will
first need to deserialize the response.Content.
//When you call the Rest endpoint, you can call it
in a few different ways.
//var response = client.Execute(request);
// 1
var response = client.Execute<IPData>(request); // 2
//var obj =
JsonConvert.DeserializeObject(response.Content);
//Console.WriteLine(obj["ip"].Value);
Console.WriteLine(response.Data.ip);
Console.WriteLine(response.Data.latitude);
Console.WriteLine(response.Data.longitude);
}
public class IPData
{
public string ip { get; set; }
public string city { 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 string latitude { get; set; }
public string longitude { get; set; }
public string flag { get; set; }
public string time_zone { get; set; }
}
}
}