简体   繁体   English

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ConsoleAppTest01.Location”,因为该类型需要 JSON ZA8CFDE6331BD59EB2AC96F8911ZC4

[英]Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleAppTest01.Location' because the type requires a JSON object

Cannot figure out why I'm getting this exception:无法弄清楚为什么我会收到此异常:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'ConsoleAppTest01.Location' because the type requires a JSON object无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ConsoleAppTest01.Location”,因为该类型需要 JSON ZA8CFDE6331BD59EB2AC96F8911ZC4

The JSON response can be viewed here (no worries I'll update the API key later): http://dataservice.accuweather.com/locations/v1/postalcodes/search?apikey=RUuBWKtaUKRJC4GtWkjGDuhStOZblr78&q=12065 The JSON response can be viewed here (no worries I'll update the API key later): http://dataservice.accuweather.com/locations/v1/postalcodes/search?apikey=RUuBWKtaUKRJC4GtWkjGDuhStOZblr78&q=12065

Here's what my code looks like:这是我的代码的样子:

public class Location
{
    public Locality[] Property1 { get; set; }
}

public class Locality
{
    public int Version { get; set; }
    public string Key { get; set; }
    public string Type { get; set; }
    public int Rank { get; set; }
    public string LocalizedName { get; set; }
    public string EnglishName { get; set; }
    public string PrimaryPostalCode { get; set; }
    public Region Region { get; set; }
    public Country Country { get; set; }
    public Administrativearea AdministrativeArea { get; set; }
    public Timezone TimeZone { get; set; }
    public Geoposition GeoPosition { get; set; }
    public bool IsAlias { get; set; }
    public Parentcity ParentCity { get; set; }
    public Supplementaladminarea[] SupplementalAdminAreas { get; set; }
    public string[] DataSets { get; set; }
}

public class Region
{
    public string ID { get; set; }
    public string LocalizedName { get; set; }
    public string EnglishName { get; set; }
}

public class Country
{
    public string ID { get; set; }
    public string LocalizedName { get; set; }
    public string EnglishName { get; set; }
}

public class Administrativearea
{
    public string ID { get; set; }
    public string LocalizedName { get; set; }
    public string EnglishName { get; set; }
    public int Level { get; set; }
    public string LocalizedType { get; set; }
    public string EnglishType { get; set; }
    public string CountryID { get; set; }
}

public class Timezone
{
    public string Code { get; set; }
    public string Name { get; set; }
    public float GmtOffset { get; set; }
    public bool IsDaylightSaving { get; set; }
    public DateTime NextOffsetChange { get; set; }
}

public class Geoposition
{
    public float Latitude { get; set; }
    public float Longitude { get; set; }
    public Elevation Elevation { get; set; }
}

public class Elevation
{
    public Metric Metric { get; set; }
    public Imperial Imperial { get; set; }
}

public class Metric
{
    public float Value { get; set; }
    public string Unit { get; set; }
    public int UnitType { get; set; }
}

public class Imperial
{
    public float Value { get; set; }
    public string Unit { get; set; }
    public int UnitType { get; set; }
}

public class Parentcity
{
    public string Key { get; set; }
    public string LocalizedName { get; set; }
    public string EnglishName { get; set; }
}

public class Supplementaladminarea
{
    public int Level { get; set; }
    public string LocalizedName { get; set; }
    public string EnglishName { get; set; }
}

and in the Main()并在 Main()

static void Main(string[] args)
    {
        string apiUrl = $"http://dataservice.accuweather.com";
        
        Console.Write("Enter ZipCode: ");
        string zip = Console.ReadLine();

        string locationUri = $"{apiUrl}/locations/v1/postalcodes/search?apikey={APIKEY}&q={zip}";            

        Location locationData = GetLocation<Location>(locationUri);
                    
    }

and GetLocation() looks like:和 GetLocation() 看起来像:

   protected static T GetLocation<T>(string uri)
    {
        T result;

        HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
        request.Method = "GET";

        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            string jsonString = reader.ReadToEnd();
            result = JsonConvert.DeserializeObject<T>(jsonString);
        }

        return result;
    }

The Json returned from accuweather starts with a [ so it must be an array but you've told JsonConvert to deserialize an object Location;从 accuweather 返回的 Json 以[开头,因此它必须是一个数组,但您已告诉 JsonConvert 反序列化 object 位置; this is not an array (the json would have to start with a { for object deser to work)这不是一个数组(json 必须以{开始,object 才可以工作)

A quick look (on a cellphone, hard to digest the full json and map it to your classes 100% on a small screen) seems like you want to deser a Locality[] , not a Location ..快速浏览(在手机上,很难消化完整的 json 和 map 在小屏幕上 100% 显示给你的班级)似乎你想要一个Locality[] ,而不是Location ..

..but I'd advise you to just paste your json into http://QuickType.io and use the classes it makes for you; ..但我建议您将 json 粘贴到http://QuickType.io并使用它为您制作的类; they'll work off the bat他们会立即工作

Or even better;甚至更好; it's quite likely accuweather publish a swagger/open api spec that you can run through something like AutoRest or nSwag and get those tools to generate a full set of client stubs for you so it's literally a single line of code to send an object to the api, get the response, read and deser it and give you the answer it's quite likely accuweather publish a swagger/open api spec that you can run through something like AutoRest or nSwag and get those tools to generate a full set of client stubs for you so it's literally a single line of code to send an object to the api ,得到回应,阅读和应得的,给你答案

Your json root is List, not object.您的 json 根目录是列表,而不是 object。 Try to use this尝试使用这个

List<Locality> locationData = GetLocation<List<Locality>>(locationUri);

暂无
暂无

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

相关问题 无法反序列化当前 JSON object 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将当前JSON数组(例如[1,2,3])反序列化为类型”,因为该类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '' because the type requires a JSON object (e.g. {“name”:“value”}) 无法将JSON数组(例如[1,2,3])反序列化为类型&#39;&#39;,因为类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) 无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型需要 JSON 数组(例如 [1,2,3] 才能正确反序列化) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 &#39;Value[]&#39;,因为该类型需要 JSON 数组(例如 [1,2,3]) - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Value[]' because the type requires a JSON array (e.g. [1,2,3]) 无法将当前 JSON objectT 反序列化为类型,因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化 - Cannot deserialize the current JSON objectT into type because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 无法将当前JSON对象反序列化为类型&#39;WindowsService1 ...需要JSON数组(例如[1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object into type 'WindowsService1…requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“ ”,因为类型需要 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})才能正确反序列化 - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“”,因为类型需要 JSON object(例如,将 {“name”} 正确地反序列化为“value”: - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM