【问题标题】:C# process JSON from ipdata.coC# 处理来自 ipdata.co 的 JSON
【发布时间】:2020-01-14 03:11:06
【问题描述】:

对 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(request);

我的 api 密钥有效。我可以进行手动查询并得到回复。现在我正在尝试从上面示例代码中名为“response”的变量中解析 JSON 数据,这就是我迷路的地方。

  1. 我正在从 CSV 文件中提取 IP 和域名

  2. 提交到 ipdata.co 并获取 JSON 文件作为回报

  3. 示例代码来自 ipdata.co 网站。除了它显示的内容之外,我不确定如何处理以 JSON 格式返回的数据,并提取我选择的特定元素,然后将结果写入磁盘。

Google 搜索让我更加困惑,所以我希望在这里得到帮助。

我有带有 IP 和域名的 CSV 文件。我将批量查询以获取纬度/经度和许多其他变量。我要解析并保存到磁盘的结果。这就是我迷路的地方,我希望有人不仅为代码编写代码,而且还帮助理解我为什么需要按照建议进行操作。

这是使用 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#,但同时还有很多东西要学,我继续学习我不知道多少。

提前谢谢你。

【问题讨论】:

  • 所以你有来自 API 的 JSON 和一些与 CSV 相关的东西。我假设您想以 CSV 格式保存来自 json 的数据。您要保存来自 JSON 的哪些信息?全部还是部分?
  • 1> 我正在从 CSV 文件中提取 IP 和域名 2> 提交到 ipdata.co 并获取 JSON 文件作为回报 3> 示例代码来自 ipdata.co 网站。除了它显示的内容之外,我还知道如何处理以 JSON 格式返回的数据,并提取我选择的特定元素,然后将结果写入磁盘。我希望能澄清它。谢谢
  • 我一直在梳理谷歌,而我得到的回报让我更加困惑,这就是我在这里问的原因。

标签: c# json geoip


【解决方案1】:

要获取您要查找的数据,您首先需要反序列化 response.Content。

当您调用 Rest 端点时,您可以通过几种不同的方式调用它,

   var response = client.Execute(request);            // 1
   var response = client.Execute<YourClass>(request); // 2

当您使用第一种方法时,您会得到一个包含状态和内容的响应。您想使用状态 (200) 来确认调用是否成功,然后您想使用 Content 来获取您需要的信息。

如果您使用第一种方法调用端点,则需要反序列化内容。

   var obj = JObject.Parse(response.Content);

现在您有了 obj,您可以使用方括号访问任何数据来访问值。从你的代码来看,应该是这样的,

   Console.WriteLine(obj["ip"].ToString());  // Prints 8.8.8.8
   Console.WriteLine(obj["time_zone"]["name"].ToString()); // Prints America/New_York

您可以类似地访问其他数据。

其他方法

如果您在进行 API 调用时选择使用类 (client.Execute&lt;MyClass&gt;(request)),则无需反序列化。您将拥有 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# 类。

【讨论】:

    【解决方案2】:

    首先谢谢你 我仍在尝试您发布的一些代码。

    这很有效,如果没有你的帮助,我是做不到的。

    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; }
        }
     }
    
    }
    

    成功了—— 一旦我更多地练习这个,我很想问更多的问题。需要先练习几天。

    【讨论】:

    • 请注意,time_zone 不是字符串(在您的班级中)。它是另一个带有字符串的对象。使用 json2csharp 查看你需要的所有类
    猜你喜欢
    • 2019-03-18
    • 2015-06-25
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多