What is the correct way to use JSON.NET to parse stream of JSON objects?(使用 JSON.NET 解析 JSON 对象流的正确方法是什么?)
问题描述
我有一个 JSON 对象流,看起来有点像这样:
{...}{...}{...}{...}...
所以基本上是一个没有任何分隔符的 JSON 对象的串联列表.使用 JSON.NET 将它们反序列化为 IEnumerable<T>
的正确方法是什么?目前我尝试了类似
var serializer = new JsonSerializer();序列化程序.CheckAdditionalContent = false;使用 (var reader = new StreamReader(stream))使用 (var jsonReader = new JsonTextReader(reader)) {reader.SupportMultipleContent = true;阅读器.Read();而(reader.TokenType!= JsonToken.None){yield return serializer.Deserialize(reader);}}
但这失败了
Newtonsoft.Json.JsonSerializationException:反序列化对象时出现意外令牌:EndObject.路径 '',第 1 行,位置 55.在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader 阅读器,类型 objectType,JsonContract 合同,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerMember,对象现有值)在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader 阅读器,类型 objectType,布尔 checkAdditionalContent)在 Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader 阅读器,类型 objectType)在 Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader 阅读器)
显然我需要在 Deserialize
调用之后移动阅读器,但我该怎么做呢?
我认为如果你稍微改变你的循环,一切都应该工作:
public IEnumerableReadJson(流流){var serializer = new JsonSerializer();使用 (var reader = new StreamReader(stream))使用 (var jsonReader = new JsonTextReader(reader)){jsonReader.SupportMultipleContent = true;而(jsonReader.Read()){yield return serializer.Deserialize<TResult>(jsonReader);}}}
请注意,当传递给此方法的 Stream
处于打开状态时,您必须遍历 IEnumerable
:
using (var stream =/* some stream */){IEnumerable<MyClass>结果 = ReadJson<MyClass>(流);foreach(结果中的变量项){Console.WriteLine(item.MyProperty);}}
示例: https://dotnetfiddle.net/Y2FLuK
JsonNet 站点上的示例:使用 JsonReader 读取多个片段p>
I have a stream of JSON objects that looks somewhat like this:
{...}{...}{...}{...}...
So basically a concatenated list of JSON objects without any separator.
What's the proper way to deserialize those into an IEnumerable<T>
using JSON.NET? At the moment I tried something like
var serializer = new JsonSerializer();
serializer.CheckAdditionalContent = false;
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader)) {
reader.SupportMultipleContent = true;
reader.Read();
while (reader.TokenType != JsonToken.None) {
yield return serializer.Deserialize<TResult>(reader);
}
}
But this fails with
Newtonsoft.Json.JsonSerializationException: Unexpected token while deserializing object: EndObject. Path '', line 1, position 55.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)
Obviously I need to move the reader after the Deserialize
call, but how do I do this?
I think if you change your loop around slightly everything should work:
public IEnumerable<TResult> ReadJson<TResult>(Stream stream)
{
var serializer = new JsonSerializer();
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
jsonReader.SupportMultipleContent = true;
while (jsonReader.Read())
{
yield return serializer.Deserialize<TResult>(jsonReader);
}
}
}
Note that you must iterate over the IEnumerable<TResult>
while the Stream
passed to this method is open:
using (var stream = /* some stream */)
{
IEnumerable<MyClass> result = ReadJson<MyClass>(stream);
foreach (var item in result)
{
Console.WriteLine(item.MyProperty);
}
}
Example: https://dotnetfiddle.net/Y2FLuK
Sample on JsonNet site: Read Multiple Fragments With JsonReader
这篇关于使用 JSON.NET 解析 JSON 对象流的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JSON.NET 解析 JSON 对象流的正确方法是什么?
- 带问号的 nvarchar 列结果 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 使用 rss + c# 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01