按 json.net 从流中反序列化多个对象

本文关键字:反序列化 对象 json net | 更新日期: 2023-09-27 18:31:16

Greatings!我需要反序列化序列化为 json 的不同对象的文件。以下是生成的文件:

{
  "Number": 1,
  "Description": "Run version with strategy data",
  "Context": "NA"
}[
  {
    "N": 0.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.0,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 1.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.2999999523162842,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 2.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.0,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 3.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.2999999523162842,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 4.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.0,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 5.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.2999999523162842,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 6.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.0,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 7.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.2999999523162842,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 8.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.0,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  },
  {
    "N": 9.0,
    "T": 2.0,
    "Adc": [
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "SpltFr": 2.2999999523162842,
    "Acc": 1.0,
    "DAcc": 0.0,
    "Acc2": 1.0,
    "OscFr": 0.5,
    "Fltr": 0,
    "CmpEr": false,
    "ErrPck": 0,
    "IndxDiff": 0,
    "Pos": 0,
    "FastAcc": [],
    "GIndx": 0,
    "Indx": 0,
    "PcTime": "0001-01-01T00:00:00"
  }
]

我正在使用 json.net 但似乎它没有按照我想要的方式工作。

StreamReader reader = new StreamReader(fileName, Encoding.GetEncoding(1251));
var serializer = new JsonSerializer() { CheckAdditionalContent = false };
var obj1 = serializer.Deserialize(reader, typeof(Type1));
var obj2 = serializer.Deserialize(reader, typeof(Type2));

此代码仅读取第一个对象,并在读取第二个对象时引发 exption。我做错了什么?json.net 能完成我的任务吗?提前感谢!

考虑到这个主题的一些建议,我将我的代码修改为这样:

using (FileStream fs = File.Open(fileName, FileMode.Open))
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(1251)))
using (JsonTextReader jr = new JsonTextReader(sr))
{
     JsonSerializer serializer = new JsonSerializer()
     {
            Formatting = Newtonsoft.Json.Formatting.Indented,
            CheckAdditionalContent = false,
            TypeNameHandling = TypeNameHandling.Auto
     };
     var data1 = serializer.Deserialize(jr, typeof(Type1));
     var data2 = serializer.Deserialize(jr, typeof(List<Type2>));
}

但它仍然无法正常工作。似乎在阅读第一个对象后,我的光标不会立即停止在它的末尾,而是在第二个对象的某个地方。因此,我的读者无法再阅读任何对象。

按 json.net 从流中反序列化多个对象

更新:最新的 JSON.NET 大师现在通过在JsonReader上设置SupportMultipleContent来支持此功能。


据我所知,JSON.NET 不支持此功能,所以我决定实现此功能。结果在 github 上,用法如下:

var reader = new JsonTextReader(
    new StreamReader(fileName, Encoding.GetEncoding(1251)));
var serializer = new JsonSerializer { CheckAdditionalContent = false };
var obj1 = serializer.Deserialize(reader, typeof(Type1));
var obj2 = serializer.Deserialize(reader, typeof(Type2));

您需要在此处显式使用JsonTextReader,因为这是保存从TextReader读取的字符的原因。

这个问题又被问了,一个很好的工作示例在这里:https://stackoverflow.com/a/26610684/3591916

这是我基于上述链接的测试代码:

var reader = new JsonTextReader(Console.In);
reader.SupportMultipleContent = true;
var serializer = new JsonSerializer();
while (reader.Read())
{
    try
    {
        var message = serializer.Deserialize<string>(reader);
        Console.WriteLine("Got message: {0}", message);
    }
    catch (Exception exception)
    {
        Console.WriteLine(exception.Message);
    }
}

希望这对某人有所帮助。