我正在尝试使用json从climatempo api获取天气信息,但是由于以下原因,我收到了此错误消息,因为其中一项是数组,因此我尝试将其更改为:
public static ClimaTempo15 FromJson(string json) => JsonConvert.DeserializeObject<ClimaTempo15>(json, NewWeatherImage15.Converter.Settings);
收件人
public static List<ClimaTempo15> FromJson(string json) => JsonConvert.DeserializeObject<List<ClimaTempo15>>(json, NewWeatherImage15.Converter.Settings);
但是我仍然遇到相同的错误
杰森在这里https://pastebin.com/pfYzihSM
Message =无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [NewWeatherImage15.Datum]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型数组或列表),可以从JSON对象反序列化。还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。
namespace NewWeatherImage15
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class ClimaTempo15
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("state")]
public string State { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("meteogram")]
public Uri Meteogram { get; set; }
[JsonProperty("data")]
public List<Datum> Data { get; set; }
}
public partial class Datum
{
[JsonProperty("date")]
public DateTimeOffset Date { get; set; }
[JsonProperty("date_br")]
public string DateBr { get; set; }
[JsonProperty("humidity")]
public Humidity Humidity { get; set; }
[JsonProperty("rain")]
public Rain Rain { get; set; }
[JsonProperty("wind")]
public Wind Wind { get; set; }
[JsonProperty("thermal_sensation")]
public ThermalSensation ThermalSensation { get; set; }
[JsonProperty("text_icon")]
public TextIcon TextIcon { get; set; }
[JsonProperty("temperature")]
public Humidity Temperature { get; set; }
[JsonProperty("cloud_coverage")]
public CloudCoverage CloudCoverage { get; set; }
[JsonProperty("sun")]
public Sun Sun { get; set; }
}
public partial class CloudCoverage
{
[JsonProperty("low")]
public long Low { get; set; }
[JsonProperty("mid")]
public long Mid { get; set; }
[JsonProperty("high")]
public long High { get; set; }
[JsonProperty("dawn")]
public CloudCoverageAfternoon Dawn { get; set; }
[JsonProperty("morning")]
public CloudCoverageAfternoon Morning { get; set; }
[JsonProperty("afternoon")]
public CloudCoverageAfternoon Afternoon { get; set; }
[JsonProperty("night")]
public CloudCoverageAfternoon Night { get; set; }
}
public partial class CloudCoverageAfternoon
{
[JsonProperty("low")]
public long Low { get; set; }
[JsonProperty("mid")]
public long Mid { get; set; }
[JsonProperty("high")]
public long High { get; set; }
}
public partial class Humidity
{
[JsonProperty("min")]
public long Min { get; set; }
[JsonProperty("max")]
public long Max { get; set; }
[JsonProperty("dawn")]
public ThermalSensation Dawn { get; set; }
[JsonProperty("morning")]
public ThermalSensation Morning { get; set; }
[JsonProperty("afternoon")]
public ThermalSensation Afternoon { get; set; }
[JsonProperty("night")]
public ThermalSensation Night { get; set; }
}
public partial class ThermalSensation
{
[JsonProperty("min")]
public long Min { get; set; }
[JsonProperty("max")]
public long Max { get; set; }
}
public partial class Rain
{
[JsonProperty("probability")]
public long Probability { get; set; }
[JsonProperty("precipitation")]
public long Precipitation { get; set; }
}
public partial class Sun
{
[JsonProperty("sunrise")]
public DateTimeOffset Sunrise { get; set; }
[JsonProperty("sunset")]
public DateTimeOffset Sunset { get; set; }
}
public partial class TextIcon
{
[JsonProperty("icon")]
public Icon Icon { get; set; }
[JsonProperty("text")]
public Text Text { get; set; }
}
public partial class Icon
{
[JsonProperty("dawn")]
public string Dawn { get; set; }
[JsonProperty("morning")]
public string Morning { get; set; }
[JsonProperty("afternoon")]
public string Afternoon { get; set; }
[JsonProperty("night")]
public string Night { get; set; }
[JsonProperty("day", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(ParseStringConverter))]
public long? Day { get; set; }
[JsonProperty("reduced", NullValueHandling = NullValueHandling.Ignore)]
public string Reduced { get; set; }
}
public partial class Text
{
[JsonProperty("pt")]
public string Pt { get; set; }
[JsonProperty("en")]
public string En { get; set; }
[JsonProperty("es")]
public string Es { get; set; }
[JsonProperty("phrase")]
public Icon Phrase { get; set; }
}
public partial class Wind
{
[JsonProperty("velocity_min")]
public long VelocityMin { get; set; }
[JsonProperty("velocity_max")]
public long VelocityMax { get; set; }
[JsonProperty("velocity_avg")]
public long VelocityAvg { get; set; }
[JsonProperty("gust_max")]
public long GustMax { get; set; }
[JsonProperty("direction_degrees")]
public long DirectionDegrees { get; set; }
[JsonProperty("direction")]
public string Direction { get; set; }
[JsonProperty("dawn")]
public WindAfternoon Dawn { get; set; }
[JsonProperty("morning")]
public WindAfternoon Morning { get; set; }
[JsonProperty("afternoon")]
public WindAfternoon Afternoon { get; set; }
[JsonProperty("night")]
public WindAfternoon Night { get; set; }
}
public partial class WindAfternoon
{
[JsonProperty("direction")]
public string Direction { get; set; }
[JsonProperty("direction_degrees")]
public long DirectionDegrees { get; set; }
[JsonProperty("gust_max")]
public long GustMax { get; set; }
[JsonProperty("velocity_max")]
public long VelocityMax { get; set; }
[JsonProperty("velocity_avg")]
public long VelocityAvg { get; set; }
}
public partial class ClimaTempo15
{
public static ClimaTempo15 FromJson(string json) => JsonConvert.DeserializeObject<ClimaTempo15>(json, NewWeatherImage15.Converter.Settings);
// public static List<ClimaTempo15> FromJson(string json) => JsonConvert.DeserializeObject<List<ClimaTempo15>>(json, NewWeatherImage15.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this ClimaTempo15 self) => JsonConvert.SerializeObject(self, NewWeatherImage15.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
long l;
if (Int64.TryParse(value, out l))
{
return l;
}
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (long)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}
}
答案 0 :(得分:4)
您的模型与json对象匹配,因此那里没有问题。
此代码应正常工作
JsonConvert.DeserializeObject<ClimaTempo15>(json, NewWeatherImage15.Converter.Settings)
确定要获取您认为要获取的JSON吗?通常,如果数组中只有一个值,那么您会从API获取一个对象而不是对象数组。
数组
"data": [
{"id": "1"},
{"id": "2"}
]
单个对象
"data":
{"id": "1"}
正在使用C#提琴:https://dotnetfiddle.net/BXKhbm