好样的!我需要反序列化序列化为 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>));
}
但它仍然无法正常工作。似乎在读取第一个对象后,我的光标不会立即停止在它的末尾,而是在第二个对象的某个位置。因此,我的读者不能再阅读任何对象了。