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

时间:2013-09-14 19:44:02

标签: c# json json.net streamreader

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));

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

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

1 个答案:

答案 0 :(得分:5)

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


据我所知,JSON.NET不支持这一点,所以我决定实现这个功能。结果是on 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读取的字符的内容。