尝试运行天气应用时的问题。试图从openweathermap网址http://samples.openweathermap.org/data/2.5/forecast?lat=7.0744&lon=79.8919&appid=a7cae8ecfab2535dec05a83525f5ac7a获取5天的预测 但是每当它试图解析json我都会得到错误。
System.Runtime.Serialization.SerializationException 的HResult = 0x80131500 Message =反序列化WeatherApp.RootObject1类型的对象时出错。无法将值“-3.23”解析为“Int32”类型。 来源= System.Private.DataContractSerialization 堆栈跟踪: 在System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader,Boolean verifyObjectName,DataContractResolver dataContractResolver) 在System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.ReadObject(XmlDictionaryReader reader) 在System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.ReadObject(Stream stream) 在System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream) 在WeatherApp.ForecastWeatherMapProxy.d__0.MoveNext()在C:\ Users \ KMora \ source \ repos \ WeatherApp \ WeatherApp \ ForecastWeatherMapProxy.cs:第23行 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在WeatherApp.MainPage.d__6.MoveNext()在C:\ Users \ KMora \ source \ repos \ WeatherApp \ WeatherApp \ MainPage.xaml.cs:第67行
内部异常1:
XmlException:无法将值“-3.23”解析为“Int32”类型。
内部异常2:
OverflowException:对于Int32,值太大或太小。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace WeatherApp
{
public class ForecastWeatherMapProxy
{
public async static Task<RootObject1> GetWeather1(double lat, double lon)
{
var http = new HttpClient();
var url = String.Format("http://api.openweathermap.org/data/2.5/forecast?lat={0}&lon={1}&units=metric&appid=5bfe62f124a556092c27cad0f9752109", lat, lon);
var response = await http.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject1));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject1)serializer.ReadObject(ms);
return data;
}
}
[DataContract]
public class Main1
{
[DataMember]
public double temp { get; set; }
[DataMember]
public double temp_min { get; set; }
[DataMember]
public double temp_max { get; set; }
[DataMember]
public double pressure { get; set; }
[DataMember]
public double sea_level { get; set; }
[DataMember]
public double grnd_level { get; set; }
[DataMember]
public int humidity { get; set; }
[DataMember]
public int temp_kf { get; set; }
}
[DataContract]
public class Weather1
{
[DataMember]
public int id { get; set; }
[DataMember]
public string main { get; set; }
[DataMember]
public string description { get; set; }
[DataMember]
public string icon { get; set; }
}
[DataContract]
public class Clouds1
{
[DataMember]
public int all { get; set; }
}
[DataContract]
public class Wind1
{
[DataMember]
public double speed { get; set; }
[DataMember]
public double deg { get; set; }
}
[DataContract]
public class Rain1
{
}
[DataContract]
public class Sys1
{
[DataMember]
public string pod { get; set; }
}
[DataContract]
public class List1
{
[DataMember]
public int dt { get; set; }
[DataMember]
public Main1 main { get; set; }
[DataMember]
public List<Weather1> weather { get; set; }
[DataMember]
public Clouds1 clouds { get; set; }
[DataMember]
public Wind1 wind { get; set; }
[DataMember]
public Rain1 rain { get; set; }
[DataMember]
public Sys1 sys { get; set; }
[DataMember]
public string dt_txt { get; set; }
}
[DataContract]
public class Coord1
{
[DataMember]
public double lat { get; set; }
[DataMember]
public double lon { get; set; }
}
[DataContract]
public class City1
{
[DataMember]
public int id { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public Coord1 coord { get; set; }
[DataMember]
public string country { get; set; }
}
[DataContract]
public class RootObject1
{
[DataMember]
public string cod { get; set; }
[DataMember]
public double message { get; set; }
[DataMember]
public int cnt { get; set; }
[DataMember]
public List<List1> list { get; set; }
[DataMember]
public City1 city { get; set; }
}
}
这是我从xaml.cs
中调用的地方
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace WeatherApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
collection = new ObservableCollection<List1>();
this.DataContext = this;
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
try
{
var position = await LocationManger.GetPostion();
RootObject myWeather = await OpenWeatherMapProxy.GetWeather(
position.Coordinate.Latitude,
position.Coordinate.Longitude);
string icon = String.Format("ms-appx:///Assets/Weather/{0}.png", myWeather.weather[0].icon);
ResultImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));
TempResultTextBlock.Text = "Temperature: " + ((int)myWeather.main.temp).ToString()+ "°C";
DescrptionResultTextBlock.Text = "Condition: " + myWeather.weather[0].description;
LocationResultTextBlock.Text = "Location: " + myWeather.name;
}
catch
{
LocationResultTextBlock.Text = "Error to get Location. Please allow access to location services " ;
}
}
public ObservableCollection<List1> collection { get; set; }
private async void Button_Click(object sender, RoutedEventArgs e)
{
var position = await LocationManger.GetPostion();
RootObject1 myWeatherForecast = await ForecastWeatherMapProxy.GetWeather1(position.Coordinate.Latitude,
position.Coordinate.Longitude);
for (int i = 0; i < 5; i++)
{
collection.Add(myWeatherForecast.list[i]);
}
ForecastGridView.ItemsSource = collection;
}
}
}
对此问题的任何建议将不胜感激。
答案 0 :(得分:0)
您需要查看从网络电话回来的原始json。您尝试将响应反序列化为&#34; RootObject1&#34;,并且具有&#34; cnt&#34;属性,定义为Int32(int)。