【发布时间】:2022-01-07 14:59:06
【问题描述】:
无法弄清楚为什么我会收到此异常:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ConsoleAppTest01.Location”,因为该类型需要 JSON 对象
可以在此处查看 JSON 响应(不用担心我稍后会更新 API 密钥): http://dataservice.accuweather.com/locations/v1/postalcodes/search?apikey=RUuBWKtaUKRJC4GtWkjGDuhStOZblr78&q=12065
我的代码如下所示:
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; }
}
在 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);
}
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;
}
【问题讨论】:
标签: c# json deserialization