【发布时间】:2023-03-12 16:38:01
【问题描述】:
{"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 没有明确的 \n 字符
【问题讨论】:
-
在那种格式中,它不是一个对象;您需要分别解析每一行并将它们添加到列表中。
-
怎么做?
-
我试过google your error message,唯一成功的是这个问题。你确定它是正确的?
标签: c# .net json .net-core azure-data-factory-2