因此,我试图对天气数据进行反序列化,但是它不起作用。 它只是给我一个错误:
“ 21.43不是有效的整数”
这是我的代码:
WebRequest request = HttpWebRequest.Create("https://api.openweathermap.org/data/2.5/weather?q=Budapest&APPID=CENSURED");
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string Weather_JSON = reader.ReadToEnd();
MessageBox.Show(Weather_JSON);
RootObject myWeather = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(Weather_JSON)
double temp = myWeather.main.temp;
label2.Text = label2.Text + temp;
我也尝试使用:
RootObject myWeather = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(Weather_JSON, new JsonSerializerSettings(){ Culture = System.Globalization.CultureInfo.InvariantCulture });
答案 0 :(得分:1)
您的RootObject的属性是什么? 天气值不能为整数,请使其为双精度,浮点型或十进制
答案 1 :(得分:0)
如果您不想更改dataType
中的Root.main.temp
,则将其转换为double
。
例如:
double temp = Double.TryParse(myweather.main.temp)
请注意,此方法可能会引发您应处理的异常。
答案 2 :(得分:0)
1首先,您必须借助Quick Type
将JSON对象反序列化为适当的C#对象。您只需将ur json对象粘贴到左侧的文本框中。它将自动将您的JSON数据转换为C#对象。哈哈哈哈容易吧?
这是反序列化后的JSON数据:
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Welcome
{
[JsonProperty("coord")]
public Coord Coord { get; set; }
[JsonProperty("weather")]
public List<Weather> Weather { get; set; }
[JsonProperty("base")]
public string Base { get; set; }
[JsonProperty("main")]
public Main Main { get; set; }
[JsonProperty("visibility")]
public long Visibility { get; set; }
[JsonProperty("wind")]
public Wind Wind { get; set; }
[JsonProperty("clouds")]
public Clouds Clouds { get; set; }
[JsonProperty("dt")]
public long Dt { get; set; }
[JsonProperty("sys")]
public Sys Sys { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("cod")]
public long Cod { get; set; }
}
public partial class Clouds
{
[JsonProperty("all")]
public long All { get; set; }
}
public partial class Coord
{
[JsonProperty("lon")]
public double Lon { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
}
public partial class Main
{
[JsonProperty("temp")]
public double Temp { get; set; }
[JsonProperty("pressure")]
public long Pressure { get; set; }
[JsonProperty("humidity")]
public long Humidity { get; set; }
[JsonProperty("temp_min")]
public double TempMin { get; set; }
[JsonProperty("temp_max")]
public double TempMax { get; set; }
}
public partial class Sys
{
[JsonProperty("type")]
public long Type { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("message")]
public double Message { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("sunrise")]
public long Sunrise { get; set; }
[JsonProperty("sunset")]
public long Sunset { get; set; }
}
public partial class Weather
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("main")]
public string Main { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("icon")]
public string Icon { get; set; }
}
public partial class Wind
{
[JsonProperty("speed")]
public double Speed { get; set; }
[JsonProperty("deg")]
public long Deg { get; set; }
}
public partial class Welcome
{
public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, QuickType.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 }
},
};
}
public static async void RefreshDataAsync()
{
//check for internet connection
if (CheckForInternetConnection())
{
string uri = "https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";
try
{
HttpResponseMessage response = await App.client.GetAsync(uri);
try
{
response.EnsureSuccessStatusCode();
var stringContent = await response.Content.ReadAsStringAsync();
welcome = Welcome.FromJson(stringContent);
FetchDataHelper.FetchUserData(welcome.User, UserModel_Data);
User_Data = welcome.User;
}
catch
{
return;
}
}
catch
{
//cannot communicate with server. It may have many reasons.
return;
}
}
}
获得“欢迎”之后。您可以显示您的数据!
注意:请勿复制和粘贴我的代码。它只是原型。您必须将代码粘贴到项目中。