5

好样的!我需要反序列化序列化为 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));

此代码仅读取第一个对象并在读取第二个对象时抛出异常。我做错了什么?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>));
}

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

4

2 回答 2

5

更新:最新的 JSON.NET 大师现在通过设置支持这SupportMultipleContent一点JsonReader


据我所知,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.

于 2013-09-15T12:11:22.513 回答
2

再次问了这个问题,这里有一个很好的工作示例: 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);
    }
}

希望这可以帮助某人。

于 2016-04-11T05:53:35.610 回答