简体   繁体   English

对于UWP Weather App中的Int32,值太大或太小

[英]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'.' 在“ var data = ...”处,出现异常“ System.Runtime.Serialization.SerializationException:' 反序列化类型为UWPWeather.RootObject的对象时发生错误。无法将值'7.25'解析为类型' INT32“。 Value was either too large or too small for an Int32" . 对于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. 更新:工作正常,将RootObject类中的某些字段更改为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. 我完全感到困惑,为什么我从解析json文件获得的RootObject类具有不正确的字段类型,但是哦,非常感谢大家。

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 . 它告诉您的是RootObject类中的内容(或者如果其中一个类具有字段的类型,则该字段中的一个类型)中具有intInt32类型的属性或字段。

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 . 您正在反序列化的数据中传入7.25 ,不能将7.25分配给int ,而只能将其分配给floatdoubledecimal 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 . 您需要更新RootObject类(或其中包含的类之一),并修复错误地设置为int的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM