4

I have a JSON that looks like this

var json = "{\"Month\":1,\"Year\":\"2016\",\"Col1\":\"\",\"Forecast\":2121.00,\"etc\":\"etc\"}";

When I tried to deserialize it and convert to datatable, 2121.00 became 2121 losing its decimals and thus resulting to making the type of the column into int.

How can I deserialize it by retaining its value so it will still retain its decimal datatype in the datatable?

btw I'm using the Newtonsoft JSon using this method..

public string DataTableToJSONWithJSONNet(DataTable table)
    {
        string JSONString = string.Empty;
        JSONString = JsonConvert.SerializeObject(table);
        return JSONString;
    }
Eraniichan
  • 121
  • 1
  • 10
  • 1
    Do you *have* to deserialize it to a datatable instead of using your own class? Also note that what you've shown isn't the JSON, but a C# string literal representation of the JSON (without leading and trailing quotes). The actual JSON wouldn't have backslashes escaping the property names - I suggest you remove those, leaving just the JSON itself. – Jon Skeet Oct 26 '16 at 06:40
  • Its the value that I came up after using this string method. I edit my post above. – Eraniichan Oct 26 '16 at 06:42
  • It's not the actual value though. It may be what you're seeing in a debugger, but if you print the value to the screen, it won't have those backslashes... – Jon Skeet Oct 26 '16 at 06:43
  • 1
    It actually became an integer, when I tried to break point at the part where I'm binding my datatable into my gridview. – Eraniichan Oct 26 '16 at 06:45
  • convert datatype to decimal if you are using double – Vivek Nuna Oct 26 '16 at 06:45
  • No, the string didn't become an integer. I'm talking about the JSON representation in your question, not what happened after you parsed it. – Jon Skeet Oct 26 '16 at 06:46
  • I've changed it to be syntactically valid C# code. The other option would be to turn it into syntactically valid JSON, as Jon suggests. – Tomalak Oct 26 '16 at 06:48
  • Don't suppose you found an answer to this. I am having a similar issue when the API call deserializes my DataTable mine is Core 3.0 call and response from 4.7.2 API – Slagmoth Aug 20 '20 at 18:45
  • Deserialize with [`JsonSerializerSettings.FloatParseHandling = FloatParseHandling.Decimal`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_FloatParseHandling.htm) as shown in [this answer](https://stackoverflow.com/a/17602364/3744182) by [oakio](https://stackoverflow.com/users/835720/oakio) to [Newtonsoft JSON- Conversion to/from DataSet causes Decimal to become Double?](https://stackoverflow.com/q/17597594/3744182). ... – dbc Dec 25 '21 at 00:40
  • But note that integer JSON values will still get deserialized as `long` so if you have a mixture of integer and decimal column values your deserialized `DataTable` might come back with `long` column types rather than `decimal`. See [.NET JSON DeserializeObject to datatable - Parsing problem](https://stackoverflow.com/q/60973783/3744182) for details. – dbc Dec 25 '21 at 00:43

0 Answers0