Newtonsoft.JSON Mono Float反序列化异常

时间:2017-01-05 12:56:00

标签: c# vb.net mono json.net raspbian

我正在尝试使用单声道在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:输入字符串'-0.2'不是有效数字。路径'body.devices [0] .modules [0] .dashboard_data.Temperature',第1行,第329位。     at Newtonsoft.Json.JsonTextReader.ParseNumber(ReadType readType)[0x00000] in:0     at Newtonsoft.Json.JsonTextReader.ParseValue()[0x00000] in:0     在Newtonsoft.Json.JsonTextReader.Read()[0x00000] in:0     在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     在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJObject(Newtonsoft.Json.JsonReader reader)[0x00000] in:0     在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Newtonsoft.Json.JsonReader reader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract,Newtonsoft.Json.Serialization.JsonProperty成员,Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember,System.Object existingValue)[0x00000] in:0     在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": [
                    3.12345675665,
                    12.12345667899
                ]
            },
            "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中它可以正常工作。

1 个答案:

答案 0 :(得分:3)

我没有来测试,但是从追溯中,似乎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,或者可能在Raspbian上有一些Mono bug,但无论哪种方式错误的文化似乎都被使用了。

作为一种解决方法,您可以尝试暂时将当前文化设置为不变文化:

    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所述,这似乎描述了类似的问题。