Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this by making a custom <code>JsonConverter</code> which will handle rounding (or discarding) the decimal values. It might look something like this:</p> <pre><code>class CustomIntConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(int)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JValue jsonValue = serializer.Deserialize&lt;JValue&gt;(reader); if (jsonValue.Type == JTokenType.Float) { return (int)Math.Round(jsonValue.Value&lt;double&gt;()); } else if (jsonValue.Type == JTokenType.Integer) { return jsonValue.Value&lt;int&gt;(); } throw new FormatException(); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } </code></pre> <p>You can then use the custom converter like this:</p> <pre><code>JsonSerializerSettings settings = new JsonSerializerSettings { Converters = new List&lt;JsonConverter&gt; { new CustomIntConverter() } }; string json = @"[2.6, 0, 4.1, 5, -3, -2.2]"; List&lt;int&gt; list = JsonConvert.DeserializeObject&lt;List&lt;int&gt;&gt;(json, settings); foreach (int val in list) { Console.WriteLine(val); } </code></pre> <p>The output of the above would be this:</p> <pre><code>3 0 4 5 -3 -2 </code></pre> <p>If you would rather the converter ignore decimal values rather than round them, replace the following line of code</p> <pre><code> return (int)Math.Round(jsonValue.Value&lt;double&gt;()); </code></pre> <p>with this:</p> <pre><code> return (existingValue ?? default(int)); </code></pre> <p>After making that change, the output of the test code above would then look like this:</p> <pre><code>0 0 0 5 -3 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload