使用 JSON.NET 解析 JSON 对象流的正确方法是什么?

What is the correct way to use JSON.NET to parse stream of JSON objects?(使用 JSON.NET 解析 JSON 对象流的正确方法是什么?)
本文介绍了使用 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 对象流的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)