C# JSON未反序列化某些值

C# JSON未反序列化某些值,c#,arrays,json,C#,Arrays,Json,我正在使用一个返回以下JSON的API {"coord":{"lon":-1.38,"lat":54.91},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":285.83,"pressure":1013,"humidity":76,"temp_min":285.15,"temp_max":286.15},"visibili

我正在使用一个返回以下JSON的API

{"coord":{"lon":-1.38,"lat":54.91},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":285.83,"pressure":1013,"humidity":76,"temp_min":285.15,"temp_max":286.15},"visibility":10000,"wind":{"speed":5.1,"deg":240},"clouds":{"all":20},"dt":1536913200,"sys":{"type":1,"id":5104,"message":0.0049,"country":"GB","sunrise":1536903391,"sunset":1536949457},"id":2636531,"name":"Sunderland","cod":200}
我调用API并将JSON作为字符串(strJson)返回,从strJson我使用以下代码来反序列化JSON并返回值

JsonResponse res = JsonConvert.DeserializeObject<JsonResponse>(strJson);
我尝试了各种方法来返回驻留在“coord”和“sys”中的数据,但我不断得到错误“无法将JSON数组(例如[1,2,3])反序列化为类型“”,因为类型需要JSON对象(例如{“name”:“value”})才能正确反序列化”

我能够使用

但是这并不有效,因为我仍然无法快速提取变量lon和Lat及其值


如果有任何帮助,我将不胜感激,因为我以前也问过类似的问题,但是我发现的答案阻止我从“天气”中提取值,所以我很困惑。

这有什么问题吗

class JsonResponse
{
    public CoordObject coord {get;set;}
}

class CoordObject
{
    public double lon {get;set;}
    public double lat {get;set;}
}
然后您应该能够访问
res.coord.lon
res.coord.lat

通过快速搜索“json detect class”,我找到了这个有用的站点:它为您的json生成了以下类:

public class Coord
{
    public double lon { get; set; }
    public double lat { get; set; }
}

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class Main
{
    public double temp { get; set; }
    public int pressure { get; set; }
    public int humidity { get; set; }
    public double temp_min { get; set; }
    public double temp_max { get; set; }
}

public class Wind
{
    public double speed { get; set; }
    public int deg { get; set; }
}

public class Clouds
{
    public int all { get; set; }
}

public class Sys
{
    public int type { get; set; }
    public int id { get; set; }
    public double message { get; set; }
    public string country { get; set; }
    public int sunrise { get; set; }
    public int sunset { get; set; }
}

public class RootObject
{
    public Coord coord { get; set; }
    public List<Weather> weather { get; set; }
    public string @base { get; set; }
    public Main main { get; set; }
    public int visibility { get; set; }
    public Wind wind { get; set; }
    public Clouds clouds { get; set; }
    public int dt { get; set; }
    public Sys sys { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public int cod { get; set; }
}
公共类协作
{
公共双lon{get;set;}
公共双lat{get;set;}
}
公共天气
{
公共int id{get;set;}
公共字符串main{get;set;}
公共字符串说明{get;set;}
公共字符串图标{get;set;}
}
公共班机
{
公共双临时{get;set;}
公共int压力{get;set;}
公共属性{get;set;}
公共双临时值{get;set;}
公共双临时最大值{get;set;}
}
公共级风
{
公共双速{get;set;}
公共整数{get;set;}
}
公共类云
{
公共int all{get;set;}
}
公共类系统
{
公共int类型{get;set;}
公共int id{get;set;}
公共双消息{get;set;}
公共字符串国家{get;set;}
公共int日出{get;set;}
公共int日落{get;set;}
}
公共类根对象
{
公共坐标{get;set;}
公共天气列表{get;set;}
公共字符串@base{get;set;}
公共Main{get;set;}
公共int可见性{get;set;}
公共风{get;set;}
公共云{get;set;}
公共int dt{get;set;}
公共系统系统{get;set;}
公共int id{get;set;}
公共字符串名称{get;set;}
公共整数cod{get;set;}
}

您需要对对象使用dynamic,请参见此处:


如果您愿意,我可以给出一个代码示例,但是页面上有一个。

很高兴它有帮助:)是的,有时可能会令人惊讶,但是如果JSON与正确的类定义不匹配,那么将“nothing”反序列化(而不是异常)是预期的默认行为。
public object coord {get; set;}
class JsonResponse
{
    public CoordObject coord {get;set;}
}

class CoordObject
{
    public double lon {get;set;}
    public double lat {get;set;}
}
public class Coord
{
    public double lon { get; set; }
    public double lat { get; set; }
}

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class Main
{
    public double temp { get; set; }
    public int pressure { get; set; }
    public int humidity { get; set; }
    public double temp_min { get; set; }
    public double temp_max { get; set; }
}

public class Wind
{
    public double speed { get; set; }
    public int deg { get; set; }
}

public class Clouds
{
    public int all { get; set; }
}

public class Sys
{
    public int type { get; set; }
    public int id { get; set; }
    public double message { get; set; }
    public string country { get; set; }
    public int sunrise { get; set; }
    public int sunset { get; set; }
}

public class RootObject
{
    public Coord coord { get; set; }
    public List<Weather> weather { get; set; }
    public string @base { get; set; }
    public Main main { get; set; }
    public int visibility { get; set; }
    public Wind wind { get; set; }
    public Clouds clouds { get; set; }
    public int dt { get; set; }
    public Sys sys { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public int cod { get; set; }
}