简体   繁体   English

反序列化 RESTSharp JSON 响应

[英]Deserialize RESTSharp JSON Response

I'm working on a project to gather data from NOAA.我正在开展一个从 NOAA 收集数据的项目。 I'm having trouble figuring out how to make the response usable.我无法弄清楚如何使响应可用。

This is what NOAA's API response looks like to my call:这就是 NOAA 对我的调用的 API 响应的样子:

{
    "metadata": {
        "resultset": {
            "offset": 1,
            "count": 38859,
            "limit": 2
        }
    },
    "results": [
        {
            "mindate": "1983-01-01",
            "maxdate": "2019-12-24",
            "name": "Abu Dhabi, AE",
            "datacoverage": 1,
            "id": "CITY:AE000001"
        },
        {
            "mindate": "1944-03-01",
            "maxdate": "2019-12-24",
            "name": "Ajman, AE",
            "datacoverage": 0.9991,
            "id": "CITY:AE000002"
        }
    ]
}

I used JSON2CSharp.com to convert the result set into my needed classes.我使用 JSON2CSharp.com 将结果集转换为我需要的类。 Below is the relevant code:下面是相关代码:

public class NOAA
{
    public class Resultset
    {
        public int offset { get; set; }
        public int count { get; set; }
        public int limit { get; set; }
    }

    public class Metadata
    {
        public Resultset resultset { get; set; }
    }
    public class Location
    {
        public string mindate { get; set; }
        public string maxdate { get; set; }
        public string name { get; set; }
        public double datacoverage { get; set; }
        public string id { get; set; }
    }
    public class RootObject
    {
        public Metadata metadata { get; set; }
        public List<Location> results { get; set; }
    }
    public class Response
    {
        IList<Metadata> metadata;
        IList<Location> results;
    }
    public void RestFactory(string Token, string Endpoint, Dictionary<string, string> Params)
    {
        // Initiate the REST request
        var client = new RestClient("https://www.ncdc.noaa.gov/cdo-web/api/v2/" + Endpoint);
        var request = new RestRequest(Method.GET);

        // Add the token
        request.AddHeader("token", Token);

        // Add the parameters
        foreach (KeyValuePair<string, string> entry in Params)
        {
            request.AddParameter(entry.Key, entry.Value);
        }

        // Execute the REST request
        var response = client.Execute(request);

        // Deserialize the response
        Response noaa = new JsonDeserializer().Deserialize<Response>(response);

        // Print to console
        foreach (Location loc in noaa)
        {
            Console.WriteLine(loc.name);
        }
    }
}

At this point, I'm just trying to print the location name to reach my next learning milestone.在这一点上,我只是想打印位置名称以达到我的下一个学习里程碑。 I'm getting the error:我收到错误:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1579  foreach statement cannot operate on variables of type 'NOAA.Response' because    'NOAA.Response' does not contain a public instance definition for 'GetEnumerator'

Other than the error, I think I don't quite understand the proper approach since the response has more than one "layer".除了错误之外,我想我不太明白正确的方法,因为响应有不止一个“层”。 Guidance?指导?

Your foreach loop is trying to call an iterator on the object itself, not the list inside it.您的 foreach 循环试图在对象本身而不是其中的列表上调用迭代器。

Try this instead试试这个


        foreach (Location loc in noaa.results)
        {
            Console.WriteLine(loc.name);
        }

I can tell you the cause of error.我可以告诉你错误的原因。 That is because noaa is not iterable.那是因为 noaa 是不可迭代的。 If you want to iterate over any object then it needs to implement IEnumerable interface.如果你想迭代任何对象,那么它需要实现 IEnumerable 接口。 This is the reason because of which noaa is not iterable.这就是 noaa 不可迭代的原因。 noaa does not inherit this interface or implement it. noaa 不继承该接口或实现它。 Do you get the same error if you use noaa.results?如果您使用 noaa.results,您会得到同样的错误吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM