簡體   English   中英

反序列化雙精度類型變量時,JsonConvert引發“無效整數”異常

[英]JsonConvert throws a 'not a valid integer' exception when deserializing double type variable

當我嘗試從JSON字符串反序列化為對象時,出現異常。

Newtonsoft.Json.JsonReaderException
Input string '106.890907 is not a valid integer. Path 'data[0].loc.coordinates[0]', line 1, position 9413.

這就是我反序列化對象的方式:

var propertiesObj =
          JsonConvert.DeserializeObject<Location>(
              jsonObject);

而且,這就是我的json的樣子

{
    "coordinates":
    [
        106.890907,
        -6.149393
    ],
    "type": "Point"
}

最后,這是我聲明模型類的方式:

Location.cs

public class Location
{
    public List<double> coordinates { get; set; }
    public string type { get; set; }
}

我已經在StackOverFlow上提到了這個問題,但是並沒有解決我的問題, link

請幫忙。 我一直無法找到任何解決方案。 謝謝。

試試這個,更新JSON:

"loc": {
"coordinates": [
  106.890907,
  -6.149393
],
"type": "Point"
}

{
"coordinates": [
  106.890907,
  -6.149393
],
"type": "Point"
}

即從Json刪除“ loc”

重新安裝Windows 10之后,該錯誤神秘地消失了。

這是本地化問題。 您可以為JsonConvert設置CultureInfo。 我認為您的系統接受逗號(,)而不是點(。)的雙精度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM