我正在尝试使用单声道在 Raspbian 下使用 Newtosoft.Json 反序列化 JObject 中的 JSON 字符串。但它不能反序列化 Float。
Dim myStationJSON As String
myStationJSON = myWC.DownloadString("https://api.netatmo.com/api/getstationsdata?access_token=" & myToken.Token.access_token)
Dim myJSON As Newtonsoft.Json.Linq.JObject
Dim myStationName As String = ""
Dim opts As New Newtonsoft.Json.JsonSerializerSettings
opts.FloatParseHandling = Newtonsoft.Json.FloatParseHandling.Double
myJSON = Newtonsoft.Json.JsonConvert.DeserializeObject(myStationJSON, opts)
我遇到了这个异常:
Newtonsoft.Json.JsonReaderException: Input string '-0.2' is not a valid number. Path 'body.devices[0].modules[0].dashboard_data.Temperature', line 1, position 329. at Newtonsoft.Json.JsonTextReader.ParseNumber (ReadType readType) [0x00000] in :0 at Newtonsoft.Json.JsonTextReader.ParseValue () [0x00000] in :0 at Newtonsoft.Json.JsonTextReader.Read () [0x00000] in :0 at Newtonsoft.Json.JsonWriter.WriteToken (Newtonsoft.Json.JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments) [0x00000] in :0 at Newtonsoft.Json.Linq.JTokenWriter.WriteToken (Newtonsoft.Json.JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJObject (Newtonsoft.Json.JsonReader reader) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in :0
JSON:
{
"body": {
"devices": [
{
"_id": "XXX",
"cipher_id": "enc:16:XXX",
"last_status_store": 1483489982,
"modules": [
{
"_id": "XXX",
"type": "NAModule1",
"last_message": 1483489978,
"last_seen": 1483489933,
"dashboard_data": {
"time_utc": 1483489933,
"Temperature": -0.2,
"temp_trend": "stable",
"Humidity": 89,
"date_max_temp": 1483489933,
"date_min_temp": 1483484498,
"min_temp": -0.4,
"max_temp": -0.2
},
"data_type": [
"Temperature",
"Humidity"
],
"module_name": "Außen",
"last_setup": 1384606905,
"battery_vp": 5870,
"battery_percent": 95,
"rf_status": 81,
"firmware": 43
},
{
"_id": "XXXX",
"type": "NAModule4",
"last_message": 1483489978,
"last_seen": 1483489933,
"dashboard_data": {
"time_utc": 1483489933,
"Temperature": 21.7,
"temp_trend": "stable",
"Humidity": 38,
"CO2": 1139,
"date_max_temp": 1483489933,
"date_min_temp": 1483485422,
"min_temp": 21.5,
"max_temp": 21.7
},
"data_type": [
"Temperature",
"CO2",
"Humidity"
],
"module_name": "Schlafzimmer",
"last_setup": 1414704430,
"battery_vp": 4876,
"battery_percent": 38,
"rf_status": 79,
"firmware": 43
}
],
"place": {
"altitude": 210,
"city": "XXX",
"country": "DE",
"timezone": "Europe/XXX",
"location": [
0,
0
]
},
"station_name": "XXX",
"type": "NAMain",
"dashboard_data": {
"AbsolutePressure": 993.7,
"time_utc": 1483489969,
"Noise": 51,
"Temperature": 21.8,
"temp_trend": "stable",
"Humidity": 44,
"Pressure": 1021.2,
"pressure_trend": "down",
"CO2": 1069,
"date_max_temp": 1483484530,
"date_min_temp": 1483485740,
"min_temp": 21.6,
"max_temp": 21.8
},
"data_type": [
"Temperature",
"CO2",
"Humidity",
"Noise",
"Pressure"
],
"co2_calibrating": false,
"date_setup": 1384607020,
"last_setup": 1384607020,
"module_name": "Wohnzimmer",
"firmware": 102,
"last_upgrade": 1440006125,
"wifi_status": 62,
"friend_users": [
"XXXX"
]
}
],
"user": {
"mail": "XXX@XXX.XXX",
"administrative": {
"country": "DE",
"reg_locale": "de-DE",
"lang": "de-DE",
"unit": 0,
"windunit": 0,
"pressureunit": 0,
"feel_like_algo": 0
}
}
},
"status": "ok",
"time_exec": 0.050105810165405,
"time_server": 1483490016
}
在 Windows 中它工作得很好。
最佳答案
我没有 raspbian进行测试,但从回溯来看,似乎 JsonTextReader.ParseNumber()
可能正在解析特定于语言环境的文化中的数字“-0.2”(您是否正在运行德语?也许?)尽管它明确尝试使用不变区域性解析数字的事实,如 source code 中所示:
double value;
if (!double.TryParse(number, NumberStyles.Float, CultureInfo.InvariantCulture, out value))
{
throw ThrowReaderError("Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
可能您使用的是 Json.NET 的 fork 版本,或者 Raspbian 上可能存在一些 Mono 错误,但无论哪种方式,似乎都使用了错误的文化。
作为解决方法,您可以尝试将当前文化暂时设置为不变文化:
Dim opts = New Newtonsoft.Json.JsonSerializerSettings() With { _
.FloatParseHandling = Newtonsoft.Json.FloatParseHandling.Double _
}
Dim myJSON as Newtonsoft.Json.Linq.JObject
Dim oldCulture = Thread.CurrentThread.CurrentCulture
Try
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture
myJSON = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Newtonsoft.Json.Linq.JObject)(myStationJSON, opts)
Finally
Thread.CurrentThread.CurrentCulture = oldCulture
End Try
如果这不起作用,请尝试使用 LANG=en_US.UTF-8
运行您的 exe,如 here 所述,这似乎描述了一个类似的问题。
关于c# - Newtonsoft.JSON Mono Float反序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41485701/