反序列化 shodan 数据

Deserialize shodan data

我在反序列化从 Shodan 获取的数据时遇到问题。下面是我从 json2csharp 得到的 类,我正在尝试创建一个匹配数组并循环遍历它们。到目前为止,我似乎已经尝试过除工作数组之外的所有内容。数据本身作为根与包含位置(带有自己的数据等)的对象匹配。除了下面我删掉了一点。 这是我的错误: 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'Shodan.Match[]',因为类型 requ 生成一个 JSON 数组(例如 [1,2,3])以正确反序列化。

var data = JsonConvert.DeserializeObject<Match[]>(allData);

{"matches": [{"product": "product", "hash": 0, "ip": 123123, "isp": "Verizon Internet Services"}], "total": 1}


public class Location
{
    public string city { get; set; }
    public string region_code { get; set; }
    public object area_code { get; set; }
    public double longitude { get; set; }
    public string country_code3 { get; set; }
    public double latitude { get; set; }
    public string postal_code { get; set; }
    public object dma_code { get; set; }
    public string country_code { get; set; }
    public string country_name { get; set; }
}

public class Options
{
}

public class Shodan
{
    public string crawler { get; set; }
    public string id { get; set; }
    public string module { get; set; }
    public Options options { get; set; }
}

public class Match
{
    public int hash { get; set; }
    public int ip { get; set; }
    public string isp { get; set; }
    public string transport { get; set; }
    public string data { get; set; }
    public string asn { get; set; }
    public int port { get; set; }
    public List<string> hostnames { get; set; }
    public Location location { get; set; }
    public DateTime timestamp { get; set; }
    public List<string> domains { get; set; }
    public string org { get; set; }
    public object os { get; set; }
    public Shodan _shodan { get; set; }
    public string ip_str { get; set; }
    public string product { get; set; }
}

public class RootObject
{
    public List<Match> matches { get; set; }
    public int total { get; set; }
}

您可以创建另一个 class 喜欢

var allData = 
{"matches": [{"product": "product", "hash": 0, "ip": 123123, "isp": "Verizon Internet Services"}], "total": 1}

public class MyMatches {
    public Match[] matches {get; set;}
}

然后在反序列化器中使用它。 var data = JsonConvert.DeserializeObject<MyMatches>(allData);

如果您提供给我们的 JSON 代码示例是正确的。


更正

刚看到RootObjectclass。 就用那个吧。