简体   繁体   English

如何编写列表<double>在 c# 中以科学计数法转换为 Json?</double>

[英]How to write List<double> to Json in scientific notation in c#?

I'm trying to serialize List to json file like this我正在尝试将 List 序列化为 json 文件,如下所示

class ScientificDoubleConverter : JsonConverter
{
  public override bool CanRead { get { return true; } }
  public override bool CanConvert(Type objectType)
  {
    return true;
  }

  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  {
    if (value is List<double> myList)
    {

      JArray myArray = new JArray();
      myList.ForEach(dblValue => myArray.Add(dblValue.ToString("E")));
      myArray.WriteTo(writer);

    }
  }
}

This works, but i get values with qoutes这有效,但我得到了 qoutes 的值

"values": ["-9,811880E-002", "2,236657E-002", "-4,020144E-001", ...

I would like no quotes and a point in value scientific notation我不想要引号和价值科学记数法

"values": [-9.811880E-002, 2.236657E-002, -4.020144E-001, ...

You're explicitly formatting the values.您正在明确格式化这些值。 Don't do that - add the elements directly to the array:不要那样做 - 将元素直接添加到数组中:

if (value is List<double> myList)
{
    JArray myArray = new JArray();
    foreach (var element in myList)
    {
        myArray.Add(element);
    }
    myArray.WriteTo(writer);
}

Or write directly to the writer:或直接写信给作者:

if (value is List<double> myList)
{
    writer.WriteStartArray();
    foreach (var element in myList)
    {
        writer.WriteValue(element);
    }
    writer.WriteEndArray();
}

This may not get to the exact representation you want (in terms of scientific notation), but it will get to a valid JSON representation of the data you're trying to serialize.这可能无法达到您想要的精确表示(就科学记数法而言),但它将获得您尝试序列化的数据的有效 JSON 表示。 Anything reading the JSON should be able to read the exact same value from the JSON.读取 JSON 的任何内容都应该能够从 JSON 读取完全相同的值。

If you absolutely have to customize the format, you can use JsonWriter.WriteRawValue , using the invariant culture to format your values.如果您绝对必须自定义格式,则可以使用JsonWriter.WriteRawValue ,使用不变的文化来格式化您的值。 But I'd strongly advise you not to.但我强烈建议你不要这样做。 I'd be really surprised at a JSON parser that can't handle the regular output of Json.NET.我会对无法处理 Json.NET 的常规 output 的 JSON 解析器感到非常惊讶。 Here's a complete example if you really, really have to do it:如果您真的必须这样做,这是一个完整的示例:

using System;
using System.Globalization;
using System.Collections.Generic;
using Newtonsoft.Json;

class Program
{    
    static void Main(string[] args)
    {
        var writer = new JsonTextWriter(Console.Out);
        var list = new List<double>
        {
            -9.811880E-002,
            2.236657E-002,
            -4.020144E-001
        };
        WriteList(writer, list);
    }

    static void WriteList(JsonWriter writer, List<double> list)
    {
        writer.WriteStartArray();
        foreach (var element in list)
        {
            writer.WriteRawValue(element.ToString("E", CultureInfo.InvariantCulture));
        }
        writer.WriteEndArray();
    }
}

Output: Output:

[-9.811880E-002,2.236657E-002,-4.020144E-001]

You're doing dblValue.ToString("E") which is why you're getting strings.你正在做dblValue.ToString("E")这就是你得到字符串的原因。

JArray.Add except Object too as argument. JArray.Add除了 Object 也作为参数。 So, you don't have to convert your double to string.因此,您不必将双精度转换为字符串。 Just directly add to array.直接加入数组即可。

Have a look here for details. 在这里查看详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM