将多行 JSON 反序列化为 C# 对象

De-Serialize multi line JSON to C# object

 {"ItemName":"8","Id":1}
 {"ItemName":"9","Id":2}

我正在从 blob 中读取 json 文件,每一行都有上述格式,并且行甚至没有用逗号分隔,而且文件中没有方括号。

当我尝试在 jsontextreader 中将 SupportMultipleContent 设置为 true 时,出现以下异常:

 Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ValueDTO]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'ItemName', line 1, position 12.

或者,如果无法解析此类 json,那么我将如何在 Azure 中配置数据工厂以使文件具有正确的 json 格式。

代码:

using (var sr = new StreamReader(stream))
{
    using (var jsonTextReader = new JsonTextReader(sr))
    {
        jsonTextReader.SupportMultipleContent = true;
        while (jsonTextReader.Read())
        {
            var data = serializer.Deserialize<T>(jsonTextReader);
            result.Add(data);
        }

    }
}

Json has no explicit \n character

JSON 的每一行本身就是一个 JSON 对象。您没有包含所有对象的 JSON 数组。

要阅读它,您只需要重写您的方法以单独反序列化每一行:

private static List<ValueDTO> LoadItems(Stream stream)
{
    var result = new List<ValueDTO>();
    using (var reader = new StreamReader(stream))
    {
        string line = null;
        while ((line = reader.ReadLine()) != null)
        {
            if (!string.IsNullOrEmpty(line))
            {
                result.Add(JsonConvert.DeserializeObject<ValueDTO>(line));
            }
        }
    }
    return result;
}

Try it online

请使用Jsonconvert反序列化方法

 List<object> myDeserializedObjList = (List<object>)Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent, typeof(List<object>));

以下代码适用于我的机器。我正在使用 Newtonsoft.Json 版本 12.0.3 目标 netcoreapp3.0.

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"
{'ItemName':'8','Id':1}
{'ItemName':'9','Id':2}
";

            var items = new List<Item>();

            var serializer = new JsonSerializer();

            using (var sr = new StringReader(json))
            {
                using (var jsonTextReader = new JsonTextReader(sr))
                {
                    jsonTextReader.SupportMultipleContent = true;
                    while (jsonTextReader.Read())
                    {
                        var data = serializer.Deserialize<Item>(jsonTextReader);
                        items.Add(data);
                    }

                }
            }

            foreach (Item item in items)
            {
                Console.WriteLine($"{item.Id}: {item.ItemName}");
            }
        }
    }

    public class Item
    {
        public string ItemName { get; set; }
        public int Id { get; set; }
    }
}