Agent_0f_things
Agent_0f_things

Reputation: 73

Value was either too large or too small for an Int32 in UWP Weather App

So, I've been doing simple weather app, where I'm showing to user current weather by getting coordinates from the device. However I ran into a problem

 public async static Task<RootObject> GetWeather(double lat, double lon)
    {
        string AppID = "e72818716be6eb65476f5f25d4d32d82";
        var http = new HttpClient();
        var url = String.Format("http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&units=metric&APPID=" + AppID, lat, lon);
        var response = await http.GetAsync(url);
        var 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;
    }

At "var data = ..." I'm getting an exception "System.Runtime.Serialization.SerializationException: 'There was an error deserializing the object of type UWPWeather.RootObject. The value '7.25' cannot be parsed as the type 'Int32'.' Value was either too large or too small for an Int32". I don't really understand where and what exactly should be changed in order for this to work.

Update: Got it working, changed some fields in RootObject class to double. I'm utterly confused why RootObject class that I got from parsing json file had incorrect field types but oh well, huge thanks to everyone.

Upvotes: 1

Views: 760

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

What it is telling you is in the RootObject class (or in the type for one of the fields if you have classes inside the class) you have a property or field that is of type int or Int32.

You are passing in 7.25 in the data you are deserializing, 7.25 can't be assigned to a int it can only be assigned to a float, double or decimal. You need to update your RootObject class (or one of the classes it holds inside of it) and fix the field that is incorrectly set up as an int.

Upvotes: 1

Related Questions