2017-01-05 83 views
1

我試圖反序列化JSON字符串中JObject機智Newtosoft.Json Raspbian下單聲道。但它無法反序列化Float。Newtonsoft.JSON單浮動反序列化異常

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 在Newtonsoft.Json.JsonTextReader.ParseNumber(ReadType readType)[0x00000]在:0 在Newtonsoft.Json。在Newtonsoft.JsonTextReader.Read()[0x00000]中:0 在Newtonsoft.Json.JsonWriter.WriteToken(Newtonsoft.Json.JsonReader reader,Boolean writeChildren,Boolean writeDateConstructorAsDate()[0x00000] in:0 at Newtonsoft.Json.JsonTextReader.Read ,布爾writeComments)[0x00000]中:0 at Newtonsoft.Json。 Serialization.JsonSerializerInternalReader.CreateJObject(Newtonsoft.Json.JsonReader讀取器)[0x00000]在:0 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Newtonsoft.Json.JsonReader讀卡器,System.Type的的objectType,Newtonsoft.Json.Serialization.JsonContract合同,Newtonsoft.Json.Serialization.JsonProperty構件,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合同,Newtonsoft.Json.Serialization.JsonProperty成員,Newtonsoft.Json.Serialization.JsonContainerContract containerContract,Newtonsoft.Json.Serialization.JsonProperty containerMember,System.Object existingValue)[0x00000]中的:0 at Newtonsoft.Json.Serialization。 JsonSerializerInternalReader.Deserialize(Newtonsoft.Json.Js onReader讀者的System.Type的objectType,布爾checkAdditionalContent)[0x00000]中: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": "[email protected]", 
     "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中它工作得很好。

+0

你能[編輯]你的問題,包括有問題的JSON,使我們有充分的[MCVE]? – dbc

+0

剛剛添加了JSON。 – TimeO84

回答

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一些單錯誤,但無論如何,錯誤的文化似乎正在被使用。

作爲一種變通方法,你可以嘗試當前區域性臨時設置爲固定區域性:

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作爲解釋here你的exe文件,這似乎來描述一個類似的問題。

+1

用「LANG = en_US.UTF-8」運行我的exe文件!非常感謝! – TimeO84