Serialization Exception: unexpected character <<

I have this simple method that allows me to get weather data when I call it an error:

System.Runtime.Serialization.SerializationException is not handled by user code HResult = -2146233076 Message = Error deserializing an object of type UWpWeather.RootObject. An unexpected '<' was detected.

public async static Task <RootObject> GetWeather(double lat, double lng) {
    var http = new HttpClient();
    var response = await http.GetAsync("http://api.openweathermap.org/data/2.5/forecast/daily?q=leeds&type=accurate&mode=xml&units=metric&cnt=3&appid= MY AIP-KEY");
    string result = await response.Content.ReadAsStringAsync();
    var serializer = new DataContractJsonSerializer(typeof (RootObject));
    var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
    var data = (RootObject) serializer.ReadObject(ms);
    return data;
}
+4
source share
2 answers

The API does not honor any HTTP content or Accept headers that you pass on request, but rather sets the type of response content based on the request string parameter.

Your start URL:

http://api.openweathermap.org/data/2.5/forecast/daily?q=leeds&type=accurate&mode= xml & units = metric & cnt = 3 & appid = MY AIP-KEY "

:

http://api.openweathermap.org/data/2.5/forecast/daily?q=leeds&type=accurate&mode= json & units = metric & cnt = 3 & appid = MY AIP-KEY "

RootObject .

: , JSON.

+1

, Xml Json . -, (json2csharp), Json , Json, , , public List<List> list { get; set; } , . .

0

Source: https://habr.com/ru/post/1675993/


All Articles