gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:08:15 25 4
gpt4 key购买 nike

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

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

最佳答案

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


据我所知,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 读取的字符。

关于c# - 通过 json.net 从流中反序列化多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18805735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com